Loading
Loading
A system exposes only stack primitives, but the application needs first-in-first-out behaviour.
Simulate a queue using two stacks. The input encodes operations: a value ≥ 0 enqueues it, and -1 dequeues and records the value.
Return the dequeued values in order.
ops = [1, 2, -1, 3, -1, -1]in [1,2], out []two enqueuesin [], out [2,1]first dequeue pours acrossout [2]1 comes off firstin [3], out [2]enqueue 3 goes to the in-stack[1, 2, 3]Sign in to keep a note here. It is free, and nobody else can see it.
Sign inExample 1
Example 2
Sign in to open hints.
Struggling is where the learning happens. Open this once you have genuinely tried, or once the hints have run out.
O(1) amortised time · O(n) space
Input. Run your code to watch it work on this input, one line at a time.
Running code is turned off on this deployment. You can still read the problem, write code, use hints and the visualizer.
Tab moves focus out of the editor. Press Ctrl+M to let Tab indent instead.
No results yet
Run your code against the sample tests, or submit to run the full suite.