Product Category Group Sizes
Updated
Problem statement
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.
Write productCategoryGroupSizes. It gets two arguments:
String products[]: all product identifiersString pairs[][]: product pairs that belong to the same category
productCategoryGroupSizes returns int[]: the sorted sizes of the final category groups.
Function
productCategoryGroupSizes(products: String[], pairs: String[][]) → int[]Examples
Example 1
products = ["A", "B", "C", "D", "E"]pairs = [["A", "B"], ["B", "C"], ["D", "E"]]return = [2, 3]The pairs link A to B and B to C, so A, B, and C form one category of size 3. D and E form a second category of size 2. Sorted in increasing order, the smaller size comes first. So the answer is the size of D and E, followed by the size of A, B, and C.
Example 2
products = ["p1", "p2", "p3", "p4"]pairs = [["p1", "p2"]]return = [1, 1, 2]The only pair puts p1 and p2 in one category of size two. Products p3 and p4 are not paired with any other product, so each is its own category of size one. In increasing order, the answer is one, one, two.
Constraints
- Product identifiers are unique in
products. - Each pair contains two valid product identifiers.
- Category membership is transitive.
More Amazon Phone Screen questions
- LRU Cache for Query ResultsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Course SchedulePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Course Schedule IIPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Minimum Time to Spread Through a GridPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Consistent-Hash Packet RouterPHONE SCREEN · Seen Nov 2026
- Count Connected ComponentsPHONE SCREEN · Seen Nov 2026
- Second Largest Distinct ElementPHONE SCREEN · Seen Nov 2026
- Task SchedulerOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Top K Frequent ElementsOA · PHONE SCREEN · Seen Nov 2026
- Number of IslandsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- Earliest Time All Users Are ConnectedUber · PHONE SCREEN · ONSITE INTERVIEWUber interview questions
- Group Transitive String AliasesApple · PHONE SCREENApple interview questions
- Bus Station Seat AllotmentInMobi · PHONE SCREENInMobi interview questions
- Find All People With the SecretGoogle · PHONE SCREENGoogle interview questions
- Accounts MergeOracle · ONSITE INTERVIEWOracle interview questions
- Reconstruct a Graph into a TreeLinkedIn · ONSITE INTERVIEWLinkedIn interview questions
- Find All People With SecretIDFC Bank · ONSITE INTERVIEWIDFC Bank interview questions
- Resolve Overlapping Price RangesSquarepoint Capital · OASquarepoint Capital interview questions
- TikTok Server Network OptimizationTikTok · OATikTok interview questions
- Unit Jobs with Deadlines and ProfitsJP Morgan Chase · OAJP Morgan Chase interview questions
Amazon interview experiences
- Amazon Experienced SDE Onsite Questions: 1 Experience
- Amazon SDE Onsite Interview Questions: 9 Experiences
- Amazon SDE II and L5 FDE Interview Questions: 4 Experiences
- Amazon Full-Time OA Problems, Earlier Reports (Part 5 of 5)
- Amazon Full-Time OA Problems, Recent Reports (Part 4 of 5)
- Amazon New Grad OA Problems: Every Task We Have (Part 3 of 5)