Compute minimum passes to collect numbers
Company: Amazon
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Online Assessment
Overview: This question evaluates a candidate's ability to analyze array traversal patterns, permutation properties, and algorithmic efficiency, specifically reasoning about linear-time operations and state tracking across multiple left-to-right passes.
Constraints
- 1 <= n <= 200000
- shelf is a permutation of integers 1..n
- Time complexity should be O(n)
- Space complexity should be O(n)
Hints
- Track the index (position) of each value 1..n in shelf.
- If position[i] < position[i+1], you can collect i and i+1 in the same pass; otherwise, you need a new pass.
- The answer equals 1 plus the number of i in [1..n-1] where position[i] > position[i+1].