FastPrepProduct Category Group Sizes

Product Category Group Sizes

Amazon logoAmazon● EasyFULLTIMEPHONE SCREEN

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:

  1. String products[]: all product identifiers
  2. String 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

Similar questions at other companies

Amazon interview experiences

See Amazon hiring insights
Saved
class Solution {
    public int[] productCategoryGroupSizes(String[] products, String[][] pairs) {
      // write your code here
    }
}
Checking account…