Compute minimum passes over permutation
Company: Amazon
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Online Assessment
Overview: This question evaluates algorithmic problem-solving skills focused on array traversal and permutation analysis, testing understanding of sequential scanning behavior and state progression within the Coding & Algorithms domain.
Constraints
- 1 ≤ n ≤ 200000
- shelf is a permutation of [1, 2, ..., n]
- Expected time complexity: O(n)
- Expected auxiliary space: O(n)
Hints
- Record the index (position) of each value in shelf.
- Consider the sequence of positions pos[1], pos[2], ..., pos[n].
- A new pass is needed whenever pos[i] < pos[i-1]. The answer is 1 plus the count of such breaks.