Data Structures & AlgorithmsHardGoldman 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), dequeue(), enqueue(1), enqueue(7), dequeue(), enqueue(3), enqueue(8). In what order are values dequeued?
- A. 4, 7
- B. 1, 4
- C. 4, 1
- D. 7, 3, 8
Correct answer: C. 4, 1
Moving every element from "in" to "out" (which reverses their order) exactly when "out" runs empty restores FIFO order overall, so dequeues return 4, 1 — the same order the values were enqueued in. Treating it as a single stack (LIFO) would wrongly give 4, 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
- Keys 26, 63, 80, 39, 14, 99, 18, 93, 44 are inserted via linear probing (h(k) = k mod 11, probe by +1 on collision) into an initially…
- A circular queue of capacity 4 starts empty and runs this sequence in order: enqueue(3), enqueue(1), enqueue(6), enqueue(9), dequeue()…
- What is the postfix (Reverse Polish) form of the infix expression "i (f + c)- a / h", built with the standard stack-based conversion (^…
- Array [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] is left-rotated by d = 1 positions using the reversal algorithm (reverse first d, reverse rest…
- 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(7)?
- Kadane's algorithm runs on the array [-4, 3, 9, 3, 3, -6, 7, 4, 6] to find the maximum sum of a contiguous subarray. What sum does it…