← Amazon Interview Insights

Amazon·Software Engineer·Technical Phone Screen·Intermediate

Intermediate
Jun 2026

Summary

Amazon SWE interview with a tricky array traversal problem that looks deceptively simple until you actually try to nail the O(n) constraint.

Questions Asked (1)

Q1

You have an array that's a permutation of 1 to n. You scan left to right collecting numbers in order starting from 1, and whenever you hit the end without finishing, you start a new full pass. How many passes does it take to collect everything, and can you do it in O(n)?

Algorithms & Data Structures
Author's notes

My first instinct was to just simulate it, which works but obviously blows up on time complexity for large inputs.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

First, clarify the problem: we need to find the minimum number of left-to-right passes to collect numbers in increasing order from 1 to n. Then, observe that a new pass is required exactly when the next number to collect appears before the current number in the array. Use an array to store the position of each number, then iterate from 1 to n, counting how many times the position decreases.

Pro tip: Mention that this is equivalent to counting the number of 'descents' in the sequence of positions when numbers are ordered by value. This insight shows you can reduce the problem to a simple linear scan, demonstrating strong algorithmic thinking.

1. Clarify the problem

Confirm that we start with a fresh pass each time we reach the end without having collected all numbers, and that within a pass we collect numbers in increasing order as they appear.

2. Identify the key observation

Realize that a new pass is needed exactly when the next number to collect (i+1) appears before the current number (i) in the array. Thus, the number of passes equals 1 plus the number of such inversions in the order of positions.

3. Design the algorithm

Create an array pos of size n+1 where pos[value] = index in the original array. Initialize passes = 1. Iterate i from 1 to n-1: if pos[i+1] < pos[i], increment passes. Return passes.

4. Analyze complexity

The algorithm uses O(n) extra space for the pos array and runs in O(n) time: one pass to build pos, one pass to count descents. This meets the O(n) requirement.

5. Test with examples

Walk through a small example, e.g., [3,1,2] (n=3). pos[1]=1, pos[2]=2, pos[3]=0. pos[2] > pos[1]? 2>1 no; pos[3] < pos[2]? 0<2 yes, so passes=2. Verify by simulation: Pass 1: collect 1, then 2? Actually, scan: 3 (skip), 1 (collect), 2 (collect) -> collected 1,2. Pass 2: collect 3. Total 2 passes.

Key Points to Mention

  • The problem reduces to counting the number of times the position of consecutive numbers decreases.
  • A new pass is required exactly when the next number to collect appears earlier in the array than the current number.
  • Using a position array allows O(1) lookup of each number's index, enabling a single linear scan.
  • The algorithm runs in O(n) time and O(n) space, which is optimal for this problem.
  • Edge cases: n=1 (answer 1), already sorted array (answer 1), reverse sorted array (answer n).
  • This approach is similar to counting the number of increasing runs in the permutation when ordered by value.

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