Solve Union-Find, Graph, and Stream Problems
AmazonSoftware Engineer · OnsiteLast updated
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.
Solve the following independent coding problems.
1. Merge Product Categories
You are given a list of tuples (productA, productB). Each tuple means the two products belong to the same category. Category membership is transitive: if A is in the same category as B, and B is in the same category as C, then A, B, and C are all in the same category.
Return:
- The total number of final categories.
- The size of each category.
Products may be represented as strings or integer IDs.
2. Validate and Produce a Course Schedule
You are given n courses labeled 0 to n - 1 and a list of prerequisite pairs (a, b), meaning course b must be taken before course a.
Return whether it is possible to complete all courses. If possible, output one valid course order. If impossible, report that a conflict cycle exists.
3. Detect a Zero-Sum Subarray
Given an integer array, determine whether there exists a non-empty contiguous subarray whose sum is 0. You only need to explain the approach.
4. Find Anagrams in a Character Stream
Characters arrive one by one in a stream. Given a target word target, report every position where the most recent len(target) characters form an anagram of target.
For example, if target = "abc", then windows such as "bca", "cab", and "abc" should be reported when they appear in the stream.
Design the solution so it works online as the stream arrives, without storing the entire stream.
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.