Practice
JavaScriptData StructuresReactConcepts
Sign in
← Back to problems

Queue From Two Stacks

Queuesmedium

Implement `queueFromStacks(values)` that simulates a FIFO queue using two stacks (arrays with push/pop only), returning the dequeue order.

Sample tests

Input: queueFromStacks([1,2,3])
Output: [1,2,3]
Input: queueFromStacks([])
Output: []

+ 1 hidden test run on Submit.

Hints

Common pitfalls
  • Refilling the out-stack while it still has items breaks the order.

Learning resources

  • Wikipedia: Data structure
Approach & explanation (try first)

Transferring between two LIFO stacks yields FIFO order; each item moves at most twice, so it is O(1) amortized per operation.

Loading...
⌘/Ctrl + Enter

Run your code to see results.