Implement a FIFO queue using only two stacks (JavaScript arrays limited topush and pop). No shift, unshift, splice, or index access allowed
on the internal storage.
API
enqueue(value) → void add to the back
dequeue() → value | null remove and return the front; null if empty
peek() → value | null front value without removing; null if empty
isEmpty() → booleansolve(ops) replays an array of operation tuples and returns the output
of every call in order. enqueue produces null in the output.
solve([
['enqueue', 1],
['enqueue', 2],
['dequeue'],
['peek'],
['isEmpty'],
])
→ [null, null, 1, 2, false]Key insight — lazy transfer: move all items from the *inbox* stack to the
*outbox* stack only when the outbox is empty and a dequeue/peek is needed.
This gives amortised O(1) per operation.
Sample tests