01 · Question
Implement a first in first out (FIFO) queue using only two stacks.
02 · Analysis
s1 holds incoming values; s2 exposes the oldest value at its top. Transfer s1 to s2 only when s2 is empty, or new arrivals would jump ahead of older ones. Each value is pushed into s1, moved once, and popped from s2 once: amortized O(1) per operation, O(n) space. One pop/peek may transfer n values and cost O(n). pop/peek require a nonempty queue. Test push 1, push 2, peek, push 3, then pop: it must return 1.