Data Structures & AlgorithmsModerateICPC regional · Amazon SDE OA
A queue holds 3, 1, 4, 2, 5 from front to back. You repeatedly look at the front element: if it is the next value needed (starting with 1), you remove it; otherwise you move it to the back. How many move-to-back operations occur before all five are removed?
- A. 0
- B. 4
- C. 3
- D. 2
Correct answer: C. 3
Simulate the queue: values come off in order, and each non-matching front element is moved back once. The count depends on the initial order relative to the needed order, which is the key invariant.
Practise more Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questionsArrays, Strings, Linked Lists, Stacks, Queues and Hashing: notes and key points
More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions
- For windows of size 3 over [3, 1, 4, 2, 5, 1, 6, 0], how many windows have their maximum at the last position of the window (ties count as…
- Four unit-time jobs given as (deadline, profit) are (4, 20), (1, 10), (1, 40), (1, 30). Each job must finish by its deadline and only one…
- What is the length of the shortest contiguous subarray which, if sorted in place, makes the whole array [2, 6, 4, 8, 10, 9, 15] sorted?
- What is the number of trailing zeros in the decimal representation of the product 10 25 4 15 50?
- How many distinct sums appear over all non-empty contiguous subarrays of [2, 1, 3, 1]?
- Ropes of lengths 4, 3, 2 and 6 must be joined pairwise into one rope; joining costs the sum of the two lengths and you may join any two…