Solve Union-Find, Graph, and Stream Problems
Company: Amazon
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Onsite
Quick Answer: This question evaluates understanding of disjoint-set (union-find) operations, directed graph processing including cycle detection and ordering, contiguous subarray sum reasoning, and online streaming/anagram detection techniques, emphasizing data structures and algorithmic problem-solving.
Part 1: Merge Product Categories
Constraints
- 0 <= len(pairs) <= 200000
- Each pair contains exactly 2 product strings
- Each product string has length between 1 and 50
- Only products appearing in pairs are included in the result
Examples
Input: [['phone', 'tablet'], ['tablet', 'laptop'], ['chair', 'desk']]
Expected Output: [2, 2, 3]
Explanation: There are two categories: {phone, tablet, laptop} of size 3 and {chair, desk} of size 2.
Input: []
Expected Output: [0]
Explanation: No products appear, so there are zero categories.
Input: [['a', 'a']]
Expected Output: [1, 1]
Explanation: A self-pair still creates one category containing only a.
Input: [['book', 'pen'], ['cup', 'plate'], ['plate', 'fork'], ['lamp', 'lamp']]
Expected Output: [3, 1, 2, 3]
Explanation: The categories are {lamp}, {book, pen}, and {cup, plate, fork}.
Hints
- Think of each product as a node in an undirected graph, and each pair as an edge.
- A Disjoint Set Union structure can merge connected components efficiently.
Part 2: Validate and Produce a Course Schedule
Constraints
- 1 <= n <= 100000
- 0 <= len(prerequisites) <= 200000
- 0 <= a, b < n for every prerequisite pair [a, b]
- Duplicate prerequisite pairs may appear
Examples
Input: (4, [[1, 0], [2, 0], [3, 1], [3, 2]])
Expected Output: [0, 1, 2, 3]
Explanation: Course 0 must come first, then 1 and 2, then 3.
Input: (2, [[1, 0], [0, 1]])
Expected Output: []
Explanation: The prerequisites form a cycle, so no valid schedule exists.
Input: (1, [])
Expected Output: [0]
Explanation: A single course with no prerequisites can be taken directly.
Input: (5, [[2, 1], [2, 1], [3, 2]])
Expected Output: [0, 1, 2, 3, 4]
Explanation: Duplicate prerequisite edges should not change the result.
Hints
- Use indegrees to track how many prerequisites each course still needs.
- If you want deterministic output, always choose the smallest available course next.
Part 3: Detect a Zero-Sum Subarray
Constraints
- 0 <= len(nums) <= 200000
- -1000000000 <= nums[i] <= 1000000000
- The array may contain positive, negative, and zero values
Examples
Input: [4, 2, -3, 1, 6]
Expected Output: True
Explanation: The subarray [2, -3, 1] sums to 0.
Input: [1, 2, 3]
Expected Output: False
Explanation: No contiguous subarray has sum 0.
Input: []
Expected Output: False
Explanation: There is no non-empty subarray in an empty array.
Input: [0]
Expected Output: True
Explanation: The single-element subarray [0] sums to 0.
Input: [1, -1, 2]
Expected Output: True
Explanation: The subarray [1, -1] sums to 0.
Hints
- Track prefix sums as you move through the array.
- If the same prefix sum appears twice, the elements between those positions sum to 0.
Part 4: Find Anagrams in a Character Stream
Constraints
- 1 <= len(target) <= 100000
- 0 <= len(stream) <= 200000
- target and stream may contain repeated characters
- The expected solution uses O(len(target)) extra space besides the output list
Examples
Input: ('abc', 'cbaebabacd')
Expected Output: [0, 6]
Explanation: The windows 'cba' and 'bac' are anagrams of 'abc'.
Input: ('aab', 'aaab')
Expected Output: [1]
Explanation: Only the window 'aab' starting at index 1 matches.
Input: ('xyz', 'xy')
Expected Output: []
Explanation: The stream is shorter than the target, so no full window exists.
Input: ('a', 'baa')
Expected Output: [1, 2]
Explanation: Each single-character window equal to 'a' is an anagram of the target.
Hints
- Use a sliding window of size len(target) and update counts as characters enter and leave the window.
- Instead of comparing two full frequency maps every time, maintain a difference map between target and the current window.