← Amazon Interview Insights

Amazon·Software Engineer·Technical Phone Screen·Intermediate

Intermediate
Apr 2026

Summary

Phone screen for a software engineer role at Amazon. One coding question, union-find territory, got through it with BFS instead and it worked fine.

Questions Asked (1)

Q1

You're given a list of pairs where each pair means two products belong to the same category. Categories are transitive. Find the total number of distinct categories and how many products are in each one.

Algorithms & Data Structures
Author's notes

Went with BFS and it passed, but I knew halfway through they were probably expecting union-find.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Model the problem as a graph where products are nodes and pairs are edges, then find connected components using Union-Find (Disjoint Set Union) for near-linear time. After processing all pairs, count the distinct roots and the size of each component to get the number of categories and product counts.

Pro tip: Mention that Union-Find with path compression and union by rank achieves almost O(1) per operation, making it ideal for large datasets. Also, clarify edge cases like duplicate pairs or self-loops, and discuss how to handle products that appear only in pairs versus those that might be listed separately.

1. Clarify the problem

Confirm that categories are transitive, meaning if A-B and B-C are pairs, then A, B, and C are in the same category. Ask if the input list can contain duplicate pairs or if products are only given via pairs.

2. Choose the right data structure

Select Union-Find (Disjoint Set Union) to efficiently group products into categories. Explain that it supports union and find operations with near-constant time complexity when optimized.

3. Process all pairs

Iterate through each pair, perform union on the two products, and ensure each product is added as a separate set initially. Use a hash map to map product IDs to parent pointers if products are not integers.

4. Count categories and sizes

After processing, traverse all products, find their root, and aggregate counts per root. The number of distinct roots is the number of categories, and the count per root is the number of products in that category.

5. Analyze complexity and edge cases

State that time complexity is O(N α(N)) where N is the number of products (or pairs), and space is O(N). Discuss handling of duplicate pairs, self-loops, and products that appear only once (if applicable).

Key Points to Mention

  • Union-Find (Disjoint Set Union) with path compression and union by rank for optimal performance.
  • Graph representation: products as nodes, pairs as edges, categories as connected components.
  • Time complexity: O(N α(N)) which is effectively linear, and space complexity O(N).
  • Handling of duplicate pairs and self-loops without affecting correctness.
  • Mapping non-integer product identifiers to indices using a hash map.
  • Edge case: products that appear only in pairs versus those that might be listed separately (if input format allows).

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.