Reported November 2019
Bloombergqueue

Implement Queue Using Stacks

Reported by candidates from Bloomberg's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live Bloomberg OA. Under 2s to a working solution.
Founder's read

The edge case that sinks a naive Implement Queue Using Stacks answer is moving elements between stacks at the wrong time. Bloomberg reported this one in November 2019, and it still shows up as a warm-up that punishes sloppy ordering. You have two stacks, four operations, and a parallel-array harness that returns strings like "null" and "true". The data structure is easy. The wiring is where people lose points. If you blank mid-assessment, StealthCoder runs invisibly on your desktop and gives you the working solution in real time as a safety net.

The problem

Implement a first-in-first-out queue using only two stacks. The queue supports push, pop, peek, and empty.
The function receives parallel arrays operations and arguments. Process them in order. MyQueue creates the queue, push uses the first integer in the corresponding argument array, and the remaining operations take no arguments.
Return one string for every operation: null for MyQueue and push, the returned integer for pop and peek, and true or false for empty.
Use only standard stack operations: push to the top, read or remove the top, size, and emptiness checks.

Function
MyQueue(operations: String[], arguments: int[][]) → String[]

Examples
Example 1
operations = ["MyQueue","push","push","peek","pop","empty"]
arguments = [[],[1],[2],[],[],[]]
return = ["null","null","null","1","1","false"]
Create the queue, push 1 and 2, then peek and pop the front value 1. One value remains, so empty returns false.

Constraints
1 <= x <= 9
At most 100 calls will be made to push, pop, peek, and empty.
All the calls to pop and peek are valid.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick is two stacks with distinct jobs: an input stack for pushes and an output stack for pops and peeks. Push always goes onto input. When you pop or peek, only refill output from input if output is empty. Then pop or read the top of output. That lazy transfer gives amortized O(1) per operation. The common pitfall is dumping input into output on every pop, or transferring while output still holds elements, which scrambles the order. The second pitfall is the harness. Output must be strings: "null" for MyQueue and push, the integer as a string for pop and peek, and "true" or "false" for empty. Empty means both stacks are empty, not just one. Trace Example 1 by hand: push 1, push 2, peek moves both over and returns 1. If you freeze on the wiring during the live OA, StealthCoder is the hedge that hands you the clean version.

If you see this problem in your OA tomorrow, the play is to recognize the pattern in 30 seconds. StealthCoder buys you that recognition.

If this hits your live OA

You can drill Implement Queue Using Stacks 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. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken.

Get StealthCoder

Related leaked OAs

⏵ Practice the LeetCode equivalent

This OA pattern shows up on LeetCode as implement queue using stacks. If you have time before the OA, drill that.

⏵ The honest play

You've seen the question. Make sure you actually pass Bloomberg's OA.

Bloomberg reuses patterns across OAs. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Implement Queue Using Stacks FAQ

How hard is Implement Queue Using Stacks really?+

It's easy on paper. The logic is about twenty lines. Most lost points come from the transfer rule and from formatting the output strings correctly. If you've written a two-stack queue once, you can finish this in a few minutes.

What's the trick to the two-stack queue?+

Keep an input stack and an output stack. Push to input. On pop or peek, move everything from input to output only when output is empty. Reversing the order once flips LIFO into FIFO, and the lazy transfer keeps it amortized constant time.

What edge case breaks a naive solution?+

Transferring while output still has elements. That puts newer items beneath older ones and returns the wrong front value. Also check empty: it must be true only when both stacks are empty, since items may sit in either one.

How do I handle the operations and arguments arrays?+

Loop by index. For MyQueue, create a fresh queue and append "null". For push, use arguments[i][0] and append "null". For pop and peek, append the integer as a string. For empty, append "true" or "false" in lowercase.

How do I prepare for this in 48 hours?+

Write the two-stack queue from memory twice, then trace Example 1 by hand. Practice the string output format too, since the harness is unusual. Constraints are tiny, so don't over-engineer. Correct ordering and the empty check matter most.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with Bloomberg.

OA at Bloomberg?
Invisible during screen share
Get it