← Amazon Interview Insights

Amazon·Software Engineer·Onsite - Coding / Algorithms·Intermediate

Intermediate
May 2026

Summary

Amazon SWE coding round, four problems back to back. Heavy on graphs and classic array tricks. Nothing too exotic but the stream anagram one had me second-guessing my approach the whole time.

Questions Asked (4)

Q1

You're given a list of product pairs where each pair means the two products share a category. Category membership is transitive. Return the total number of distinct categories and the size of each one.

Algorithms & Data Structures
Author's notes

Classic union-find setup.

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-constant time operations. The number of components gives the number of categories, and the size of each component gives the category sizes.

Pro tip: Mention that Union-Find with path compression and union by rank is optimal for dynamic connectivity and scales well for large inputs, which is crucial for Amazon-scale data.

1. Clarify requirements and edge cases

Confirm that pairs are undirected, transitivity applies, and handle cases like empty input, self-pairs, or duplicate pairs. Ask if products are identified by integers or strings.

2. Choose the right data structure

Select Union-Find (Disjoint Set Union) to efficiently group products into categories. Alternatively, consider BFS/DFS on an adjacency list if the graph is sparse and static.

3. Implement Union-Find with optimizations

Initialize each product as its own parent. For each pair, union the two sets using union by rank/size and path compression to keep operations nearly O(1).

4. Count categories and sizes

After processing all pairs, iterate through all products, find their root, and tally the size of each root. The number of distinct roots is the number of categories.

5. Analyze complexity and discuss trade-offs

State time complexity: O(N + M α(N)) where N is number of products, M is number of pairs, and α is the inverse Ackermann function. Space: O(N). Compare with BFS/DFS which is O(N+M) but may use more memory for adjacency lists.

Key Points to Mention

  • Union-Find (Disjoint Set Union) with path compression and union by rank
  • Connected components in an undirected graph
  • Time complexity: near O(N + M) due to inverse Ackermann function
  • Handling edge cases: empty input, duplicate pairs, self-pairs
  • Alternative approaches: BFS/DFS with adjacency list and their trade-offs
  • Scalability for large datasets (Amazon context)

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

Q2

Given n courses and a list of prerequisite pairs, determine if all courses can be completed. If yes, output a valid ordering. If not, explain why.

Algorithms & Data Structures
Author's notes

Topological sort with cycle detection.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Model the courses and prerequisites as a directed graph, then perform a topological sort. If a cycle exists, not all courses can be completed; otherwise, the topological order gives a valid sequence.

Pro tip: Clarify edge direction (prerequisite -> course) upfront and mention that Kahn's algorithm naturally detects cycles when the processed count is less than n. This shows attention to detail and algorithmic insight.

1. Model as a graph

Represent each course as a node and each prerequisite pair (a, b) as a directed edge from a to b, meaning a must be taken before b.

2. Choose topological sort algorithm

Select either Kahn's algorithm (BFS with in-degree) or DFS with cycle detection. Explain the trade-offs briefly.

3. Execute algorithm and detect cycles

Run the algorithm, tracking visited nodes and detecting back edges (DFS) or nodes with non-zero in-degree (Kahn's). If a cycle is found, return false.

4. Return ordering or failure

If no cycle, return the topological order as a valid course sequence. Otherwise, explain that a cycle prevents completion.

Key Points to Mention

  • Directed graph representation and edge direction (prerequisite -> course)
  • Topological sorting concept and its applicability
  • Cycle detection as the key to determining feasibility
  • Kahn's algorithm (BFS) with in-degree tracking
  • DFS with recursion stack for cycle detection
  • Time and space complexity: O(V+E) time, O(V+E) space

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

Q3

Given an integer array, does any non-empty contiguous subarray sum to zero? Just explain the approach, no need to code it.

Algorithms & Data Structures
Author's notes

Prefix sums.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Use a prefix sum approach: iterate through the array while maintaining a running sum, and check if the current sum has been seen before. If it has, the subarray between the previous occurrence and the current index sums to zero. This yields an O(n) time and O(n) space solution.

Pro tip: Mention that this problem is equivalent to finding two equal prefix sums, and that the same technique extends to finding a subarray with sum equal to any target K. Also, note that if the array contains a zero, the answer is trivially true, but the prefix sum method handles it naturally.

1. Clarify the problem

Confirm that the subarray must be non-empty and contiguous, and that the array can contain negative numbers, zeros, and duplicates. Ask if the array can be empty or if there are any constraints on size.

2. Explain the prefix sum idea

Define prefix sum at index i as the sum of elements from 0 to i. A subarray from i+1 to j sums to zero if and only if prefix sum at j equals prefix sum at i.

3. Describe the algorithm

Initialize a hash set with 0 to handle subarrays starting at index 0. Iterate through the array, updating the running sum. If the sum is already in the set, return true; otherwise, add it to the set. If the loop completes, return false.

4. Analyze complexity

State that the time complexity is O(n) because each element is processed once, and space complexity is O(n) in the worst case for the hash set.

5. Discuss edge cases and alternatives

Mention edge cases: empty array (return false), array with a zero (return true), and all positive numbers (return false). Briefly note that a brute-force O(n^2) approach exists but is inefficient.

Key Points to Mention

  • Prefix sum concept and its relation to zero-sum subarrays
  • Using a hash set to store seen prefix sums for O(1) lookups
  • Initializing the set with 0 to cover subarrays starting at index 0
  • Time and space complexity analysis (O(n) time, O(n) space)
  • Handling edge cases like empty array, zeros, and all positives
  • Extension to finding subarrays with sum equal to a target K

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

Q4

Characters arrive one at a time from a stream. Given a target word, report every position where the last len(target) characters form an anagram of the target. Design it to work online without storing the full stream.

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

Sliding window with a frequency counter diff.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Use a sliding window of the last len(target) characters, maintaining a frequency count of characters in the window and comparing it to the target's frequency count. To avoid O(k) comparison per step, track the number of matching characters or use a rolling hash of frequencies. Report the current position whenever the window's frequency map matches the target's.

Pro tip: Mention that you can optimize the comparison by maintaining a count of characters that have the correct frequency, reducing the per-step check to O(1). Also, clarify that the stream is processed online, so you only store the window and frequency maps, not the entire stream.

1. Clarify requirements and constraints

Confirm that the stream is infinite, characters arrive one at a time, and we need to report positions where the last len(target) characters form an anagram. Discuss memory constraints and whether the alphabet is fixed (e.g., ASCII).

2. Design the sliding window and frequency maps

Maintain a frequency map for the target and for the current window of size len(target). Use a queue or circular buffer to efficiently add new characters and remove old ones as the window slides.

3. Optimize the anagram check

Instead of comparing full frequency maps each step, track the number of characters whose frequencies match between the window and target. Update this count incrementally when adding or removing a character.

4. Process the stream and report positions

For each incoming character, update the window and the match count. When the window size equals len(target) and the match count equals the number of distinct characters in the target, report the current position.

5. Analyze complexity and trade-offs

State that time complexity is O(n) for n characters, with O(1) per character (assuming fixed alphabet). Space complexity is O(k) for the frequency maps, where k is the alphabet size. Discuss alternative approaches like rolling hash and their trade-offs.

Key Points to Mention

  • Sliding window technique to maintain the last len(target) characters.
  • Frequency maps (hash maps or arrays) for the target and the current window.
  • Incremental update of a match count to achieve O(1) anagram check per character.
  • Handling of edge cases: target length 1, empty target, characters not in target, and stream shorter than target.
  • Time and space complexity analysis: O(n) time, O(1) space (for fixed alphabet).
  • Online processing: no need to store the entire stream, only the window and frequency maps.

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