← Amazon Interview Insights

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

Intermediate
Apr 2026

Summary

Amazon SWE coding round, three algorithmic problems back to back. The questions ranged from binary search variants to tree traversal to a classic DP problem with a harder follow-up. Pretty standard for this kind of loop but the third one had some teeth.

Questions Asked (3)

Q1

You have a sorted array where every element appears exactly twice except one. Find that single element in O(log n) time and O(1) space.

Algorithms & Data Structures
Author's notes

Binary search on sorted arrays is something I practice, but the twist here is noticing that before the single element, pairs start at even indices, and after it they shift to odd.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Use binary search to find the single element by leveraging the property that before the single element, pairs start at even indices, and after it, pairs start at odd indices. At each step, check the middle element and its pair to decide which half contains the single element.

Pro tip: Always clarify edge cases like empty array, single element, or when the single element is at the boundaries. Also, mention that the array length must be odd, and ensure your binary search handles integer overflow when computing mid.

1. Understand the problem and constraints

Restate the problem: sorted array, all elements appear twice except one, find it in O(log n) time and O(1) space. Confirm the array length is odd and elements are integers.

2. Identify the pattern

Explain that before the single element, for every pair, the first occurrence is at an even index and the second at an odd index. After the single element, this parity flips.

3. Design binary search

Initialize low=0, high=n-1. While low < high, compute mid. Ensure mid is even by decrementing if odd. Compare nums[mid] with nums[mid+1]. If equal, the single element is to the right; else, it is at mid or to the left.

4. Handle edge cases and return

When low == high, return nums[low]. Also consider cases where the single element is at the start or end, and adjust the search accordingly.

5. Analyze complexity

State that the algorithm runs in O(log n) time due to halving the search space, and uses O(1) extra space.

Key Points to Mention

  • The array is sorted, which allows binary search.
  • The parity of indices for pairs changes after the single element.
  • Binary search reduces the search space by half each iteration.
  • Time complexity is O(log n) and space complexity is O(1).
  • Edge cases: single element at the beginning, end, or middle.
  • Ensure mid is even to correctly compare pairs.

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

Q2

Given the root of an N-ary tree and a sequence of integers, check whether that sequence represents a valid path starting from the root. Values are not unique. Return true or false and analyze complexity.

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

The non-unique values part is what makes this annoying.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Clarify that the sequence must start at the root and follow parent-child edges in order. Then propose a recursive or iterative traversal that matches each value while handling duplicates by exploring all possible matching children. Finally, analyze time and space complexity, noting worst-case scenarios.

Pro tip: Explicitly discuss how duplicate values force backtracking and that the worst-case time can be O(n * m) where n is tree size and m is sequence length, but often much better. Mention that you can optimize by precomputing paths or using BFS/DFS with early termination.

1. Clarify the problem

Confirm that the sequence must start at the root and each consecutive value must be a child of the previous node. Ask about edge cases: empty sequence, single node, duplicates, and whether the path must end at a leaf.

2. Choose traversal strategy

Decide between recursive DFS or iterative BFS/DFS. Explain that due to duplicates, you may need to explore multiple branches, so backtracking or maintaining a set of candidate nodes is necessary.

3. Implement matching logic

Start with the root if it matches the first value; then for each subsequent value, check if any child of the current candidate nodes matches. Use a queue or recursion to track candidates.

4. Handle duplicates and early termination

When multiple children match, branch the search. If at any point no candidates remain, return false. If the sequence is exhausted, return true (optionally check if current node is a leaf if required).

5. Analyze complexity

Time: O(n * m) worst-case where n is number of nodes and m is sequence length, but often O(n) if no duplicates. Space: O(h) for recursion stack or O(w) for queue, where h is height and w is max width.

Key Points to Mention

  • Duplicates require exploring multiple branches, so simple greedy matching fails.
  • Use DFS with backtracking or BFS with a queue of candidate nodes.
  • Edge cases: empty sequence, sequence longer than depth, root value mismatch.
  • Time complexity: worst-case O(n * m) due to branching; average O(n) if values unique.
  • Space complexity: O(h) for recursion or O(w) for BFS queue.
  • Optimization: early termination when candidates empty or sequence exhausted.

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

Q3

Given two integer arrays, find the maximum length of a contiguous subarray that appears in both. Walk through multiple approaches and their trade-offs.

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

Started with DP, which is the obvious move, O(n*m) time and space.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by clarifying the problem (contiguous subarray, common to both arrays) and then present a progression of solutions: brute force, dynamic programming, and optimized approaches like binary search with hashing. For each, explain time/space complexity and trade-offs, and discuss which is best for Amazon's scale.

Pro tip: Amazon values practical optimization and customer obsession. Emphasize how your chosen approach scales with large inputs and mention potential real-world applications, such as finding common patterns in user behavior logs.

1. Clarify the problem

Confirm that 'contiguous subarray' means a contiguous sequence of elements within each array, and that the subarray must appear in both arrays (not necessarily at the same indices). Ask about constraints like array sizes and value ranges.

2. Brute force approach

Describe checking all possible subarrays of one array and seeing if they appear in the other. This is O(n^3) or O(n^2 * m) time, which is inefficient but serves as a baseline.

3. Dynamic programming approach

Use a 2D DP table where dp[i][j] = length of longest common subarray ending at A[i] and B[j]. If A[i] == B[j], dp[i][j] = dp[i-1][j-1] + 1, else 0. Time and space O(n*m).

4. Optimized approach with binary search and hashing

Binary search on the length L. For a given L, hash all subarrays of length L in one array and check if any hash exists in the other. Use rolling hash to achieve O((n+m) log(min(n,m))) time.

5. Compare trade-offs and choose

Discuss when to use each: DP is simple but memory-heavy; binary search with hashing is faster for large arrays but has collision risk. Mention that for Amazon-scale data, the optimized approach is preferable.

Key Points to Mention

  • Time and space complexity of each approach
  • Dynamic programming recurrence relation
  • Rolling hash technique for subarray comparison
  • Binary search on the answer (length of subarray)
  • Handling hash collisions (e.g., double hashing or verification)
  • Edge cases: empty arrays, no common subarray, all elements same

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