Implement Stack Using Two Queues
Reported by candidates from Bloomberg's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Bloomberg reported this one in October 2022, and it's a design question wearing a stack costume. Strip the wrapper and it's two queues plus a bookkeeping rule. The twist is that the problem wants push and top in O(1), which flips the usual textbook answer. If you've memorized the version where push is the expensive call, you'll write the wrong thing under pressure. Pop absorbs the cost here. The input is a list of command strings and the output is a list of result strings. If the assessment catches you cold, StealthCoder runs invisibly as a safety net while you work through it.
The problem
Implement a last-in-first-out stack using exactly two FIFO queues. Process commands: push x: push x and append "null". pop: remove and append the top integer. top: append the top integer without removing it. empty: append "true" or "false". Design push and top to run in O(1). Function processStackUsingQueues(operations: String[]) → String[] Examples Example 1 operations = ["push 1","push 2","top","pop","empty"] return = ["null","null","2","2","false"] Two is the most recently pushed value. Constraints 1 <= operations.length <= 10^4. Pop and top are called only on a nonempty stack.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick: push always goes straight into the main queue, and you keep a separate variable holding the last pushed value so top is O(1). Pop is where you pay. Move all but the last element from the main queue into the second queue, remove the final element as the answer, then swap the queue references. After the swap, update your tracked top to the last element you moved, since that's the new top. The common pitfall is forgetting that update, so top returns a stale value right after a pop. Another one is mishandling the output format. Push appends "null", empty appends "true" or "false" as strings, and pop and top append the integer as a string. With up to 10^4 operations, O(n) pop is fine. If you blank on the swap logic, StealthCoder is the hedge during the live OA.
StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.
You can drill Implement Stack Using Two Queues cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. If you're reading this with an OA window open, you're who this was built for.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as implement stack using queues. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass Bloomberg's OA.
Bloomberg reuses patterns across OAs. If you're reading this with an OA window open, you're who this was built for. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Implement Stack Using Two Queues FAQ
What's the trick to Implement Stack Using Two Queues with O(1) push and top?+
Make push a plain enqueue and store the last pushed value in a variable for top. Put the cost in pop: shift all but the last element to the second queue, return the last one, then swap queues. Update the stored top to the final element you shifted.
How hard is this problem really?+
Easy on paper, but the O(1) push and top requirement trips people who memorized the other variant. The logic is about 20 lines. Most failures come from stale top tracking after a pop or from wrong output strings, not from the core idea.
What does the output array look like for each command?+
Every command adds one string. Push adds "null". Pop adds the removed integer as a string. Top adds the current top as a string. Empty adds "true" or "false". Match the example exactly: push 1, push 2, top, pop, empty gives null, null, 2, 2, false.
Do I need to handle pop or top on an empty stack?+
No. The constraints say pop and top are only called on a nonempty stack. You still need empty to work, so check whether the main queue has zero elements. Skip defensive error handling and keep the code short.
How do I prepare for this in 48 hours?+
Write the solution from scratch twice, once with push-heavy cost and once with pop-heavy cost, so you can switch based on the stated requirement. Then test with push, push, pop, top to confirm the tracked top updates correctly after the swap. That sequence catches most bugs.