Reported May 2026
Amazonunion find

Product Category Group Sizes

Reported by candidates from Amazon's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live Amazon OA. Under 2s to a working solution.
Founder's read

Amazon reported this one in May 2026, and it looks like a string-matching problem until you squint. It's connected components in disguise. Products are nodes, pairs are edges, and the answer is the size of every component, sorted. The hash-table hint is real: you need a map from product name to an index or parent. If you've got an Amazon OA coming up, this is the kind of question where the pattern is easy once you name it and painful if you don't. StealthCoder sits invisibly on your screen as a safety net if you blank on the setup mid-assessment.

The problem

You are given a list of products and a list of pairs. Each pair means the two products belong to the same category. Category membership is transitive: if product A is in the same category as B, and B is in the same category as C, then A and C are in the same category.
Return the size of every final category group, sorted in increasing order. The number of returned sizes is the number of final categories.

Function
productCategoryGroupSizes(products: String[], pairs: String[][]) → int[]
Complete the function productCategoryGroupSizes in the editor.
productCategoryGroupSizes has the following parameters:
String products[]: all product identifiers
String pairs[][]: product pairs that belong to the same category
Returns int[]: the sorted sizes of the final category groups

Examples
Example 1
products = ["A", "B", "C", "D", "E"]
pairs = [["A", "B"], ["B", "C"], ["D", "E"]]
return = [2, 3]
A, B, and C form one category of size 3. D and E form one category of size 2.
Example 2
products = ["p1", "p2", "p3", "p4"]
pairs = [["p1", "p2"]]
return = [1, 1, 2]
Products p3 and p4 are not paired with any other product, so each is its own category.

Constraints
Product identifiers are unique in products.
Each pair contains two valid product identifiers.
Category membership is transitive.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick is union-find with a hash map. Map each product string to a parent (or to an index), union the two products in every pair, then count how many products share each final root. Sort those counts ascending and return them. A DFS or BFS over an adjacency map works too, but union-find is shorter and has fewer ways to break. The common pitfall is forgetting products that appear in no pair. They still count as groups of size 1, so seed every product from the products list, not just from the pairs. Another trap is counting sizes before path compression settles, so call find on every product at the end. Sorting is the only extra step. If you freeze on the structure during the live OA, StealthCoder can hand you the union-find skeleton so you only have to verify it against the two examples.

The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.

If this hits your live OA

You can drill Product Category Group Sizes 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 for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play.

Get StealthCoder

Related leaked OAs

⏵ The honest play

You've seen the question. Make sure you actually pass Amazon's OA.

Amazon reuses patterns across OAs. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Product Category Group Sizes FAQ

How hard is Product Category Group Sizes really?+

It's a medium at most. It's a standard connected-components problem with string keys. If you know union-find or DFS on an adjacency map, you can write it in about fifteen lines. The difficulty is recognizing it, not implementing it.

What's the trick to solve it fast?+

Initialize every product as its own group, union each pair, then count members per root and sort. Use a hash map for parents since the identifiers are strings. Don't build counts from pairs alone or you'll miss isolated products.

Should I use union-find or DFS?+

Either works. Union-find is less code and avoids recursion depth issues on large inputs. DFS needs an adjacency map and a visited set. Pick whichever you can write without bugs under pressure, then test on both examples.

What edge cases break most solutions?+

Products with no pairs, which must return size 1. Duplicate pairs, which union-find handles fine. A pair repeated in reverse order is the same. Also make sure you sort ascending at the end, since the order of components is arbitrary.

How do I prepare for this in 48 hours?+

Write union-find with path compression from memory twice, once with integer indices and once with a string-keyed map. Then practice the count-by-root and sort step. That covers this problem and most grouping questions Amazon might reuse.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with Amazon.

OA at Amazon?
Invisible during screen share
Get it