Data Structures & AlgorithmsVery hardGoldman Sachs OA · LeetCode medium-hard
A queue is implemented with two stacks (enqueue pushes to stack "in"; dequeue pops from stack "out", refilling "out" from "in" by popping/pushing when "out" is empty) and runs: enqueue(4), enqueue(7), enqueue(8), dequeue(), enqueue(7), dequeue(), dequeue(). In what order are values dequeued?
- A. 4, 7, 8
- B. 7
- C. 8, 7, 7
- D. 8, 7, 4
Correct answer: A. 4, 7, 8
Moving every element from "in" to "out" (which reverses their order) exactly when "out" runs empty restores FIFO order overall, so dequeues return 4, 7, 8 — the same order the values were enqueued in. Treating it as a single stack (LIFO) would wrongly give 8, 7, 7.
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
- Floyd's cycle detection runs on a linked list 1 → 2 → ... → 8 whose last node's next pointer points back to node 4 (0-based position 3)…
- Array [36, 53, 71, 81, 12, 18, 20, 26] is a sorted array rotated at pivot 4. Using the standard O(log n) rotated-array binary search…
- Binary search for first/last occurrence runs on the sorted array [2, 7, 9, 11, 16, 16, 17, 18] for target 16. What (first index, last…
- A recursive function is defined as f(0) = f(1) = 1 and f(n) = f(n−1) + 2·f(n−2) for n ≥ 2. Using memoization, what is f(6)?
- A min-stack (push/pop O(1), getMin O(1) via an auxiliary stack tracking the running minimum) runs this sequence in order: push(-4), pop()…
- Binary search for first/last occurrence runs on the sorted array [1, 5, 6, 7, 10, 13, 18, 19, 19, 20, 21] for target 19. What (first…