Data Structures & AlgorithmsHardGATE CS · DSA
A queue is built from two stacks: enqueue pushes onto IN; dequeue pops from OUT, first moving every element of IN onto OUT only when OUT is empty. For the operations enq 23, enq 40, enq 11, enq 75, deq, enq 25, deq, enq 36, enq 17, deq, enq 96, deq, how many element moves from IN to OUT happen in total, and what is the last value dequeued?
- A. 4 moves; 75
- B. 4 moves; 96
- C. 4 moves; 23
- D. 8 moves; 75
Correct answer: A. 4 moves; 75
Elements move only when OUT runs dry, and each element moves at most once (amortised O(1)). Tracing gives 4 moves; dequeues return 23, 40, 11, 75 in FIFO order.
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
- Rearrange a linked list so that nodes at odd POSITIONS (1st, 3rd, …) come first, followed by the even-position nodes, keeping relative…
- Sort [−4, 3, −3, −5, −4, 5, 2] and, for each i, move two pointers to find the triplet sum closest to -2. What is that closest sum?
- Insertion sort (ascending; shift larger elements right, one key comparison per inner-loop test, the loop stops at the first element ≤ the…
- How many contiguous subarrays of [−2, 0, 5, −1, 4, 3, −1, −2, −3] have sum exactly 4? (Prefix sums with a hash map of prefix-sum…
- For s = "cdbccdddbaddca", what is the length of the longest substring with at most 2 distinct characters, and how many substrings (by…
- For s = "bcadbdaccdddbd", what is the length of the longest substring with at most 2 distinct characters, and how many substrings (by…