Find Most Frequently Purchased Products
Reported by candidates from HSBC's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The grocery manager at tagGrocery wants products bought by at least K customers, and your HSBC OA from June 2026 hands you that exact setup. It's a counting problem dressed up as a story. Each customer's bag is a row, and you need productIDs that show up in at least K different rows, printed sorted. Example 1 returns [2, 3, 8] for K = 3. It looks easy, and it is, unless you miss one detail about duplicates inside a bag. If you freeze on the live OA, StealthCoder runs invisibly as a safety net and gives you the solution on screen.
The problem
The manager of a grocery store tagGrocery wishes to determine which products are most popular with his customers (i.e. which products they purchase most frequently). The manager selects N customers who purchase a shopping bag of items containing M products, each labeled with a productID. By analyzing these M products, the manager wishes to find the productIDs of the distinct products that get purchased most frequently by at least K(given) customers. Write an algorithm to help the manager find the productIDs of the products that are most frequently purchased by the K customers. Input The first line of the input consists of two space-separated integers - tag_row, tag_col, representing the number of customers (N), the number of products in the shopping bag of each customer (M). The next N lines consist of M space-separated integers - tag[0], tag[1].........., tag[M-1], representing the productIDs of the products contained in the shopping bag of each customer. The last line consists of an integer- K_input, representing number of K customers who are chosen by the manager (K). Output Print space-separated integers representing the lexicographically sorted productIDs of the products that are most frequently purchased by the K customers. Function findMostFrequentlyPurchasedProducts(tag: int[][], K: int) → int[] Examples Example 1 tag = [[1, 2, 3, 2], [2, 3, 4, 8], [8, 3, 11, 12], [2, 3, 6, 8]] K = 3 return = [2, 3, 8] The products with ProductIDs 2, 3 and 8 are purchased by at least K customers. So, the output is [2 3 8]. Constraints 1 <= tag_row <= 10^3 1 <= tag_col <= 10^3 1 <= K_input < tag_row 0 <= tag[0], tag[1],.........., tag[N-1] <= 10^9
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is counting customers, not occurrences. Look at row one in the example: product 2 appears twice in the same bag. If you count raw occurrences, you'll overcount and return wrong IDs. Fix it by converting each row to a set, then incrementing a hash map once per distinct product per customer. After all N rows, keep every productID whose count is >= K, sort ascending, and print space-separated. Complexity is O(N*M) for counting plus O(P log P) for sorting P distinct products, which is fine for 10^3 by 10^3. Watch the IDs going up to 10^9, so use a map, not an array index. Also note the statement says 'at least K', so use >= and not ==. If you blank on the live OA, StealthCoder is the hedge that gives you this solution in real time.
If this hits your live OA and you blank, StealthCoder solves it in seconds, invisible to the proctor.
You can drill Find Most Frequently Purchased Products cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass HSBC's OA.
HSBC reuses patterns across OAs. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Find Most Frequently Purchased Products FAQ
What's the trick in the HSBC Find Most Frequently Purchased Products problem?+
Count distinct customers per product, not total occurrences. Dedupe each customer's bag with a set before updating the frequency map. In Example 1, product 2 appears twice in one bag, and counting it twice would break the result for borderline K values.
What data structure should I use?+
A hash map from productID to customer count, plus a set per row for deduping. ProductIDs go up to 10^9, so a plain array indexed by ID won't work. After counting, collect keys with count >= K and sort them ascending.
Is the condition 'at least K' or exactly K?+
At least K. The statement says products purchased by at least K customers, and the example confirms it with K = 3. Use count >= K when filtering. Using equality is a common mistake that fails hidden tests where some products exceed K.
How hard is this problem really?+
Easy. It's a hash map counting problem with a sort at the end. The only trap is duplicates within one bag. With N and M up to 10^3, an O(N*M) pass plus a sort runs comfortably, so no clever optimization is needed.
How do I prepare for this in 48 hours?+
Write the solution once from scratch: read N and M, loop rows, set-dedupe, count in a map, filter by K, sort, print. Test with Example 1 and a case with repeated IDs in a row. Also practice parsing the input format, since that's where people lose time.