Software Engineer (SDE)dsabeginner3 interactive MCQs in app
Queue & Deque
FIFO invariant vs LIFO stackcircular buffer / deque O(1) operationstwo-stack queue amortized O(1) transfer rule
Core Concept
A Queue is FIFO (First-In, First-Out): `enqueue` adds to the back and `dequeue` removes from the front, preserving arrival order—critical for BFS shortest-path waves, job scheduling, and rate-limiter sliding timestamps. Use a circular buffer or doubly linked `deque` so BOTH ends run in O(1) time (never shift an array from index 0). You can also implement an amortized O(1) queue using two stacks (`in_stack` for push, `out_stack` popped on dequeue, transferring only when `out_stack` is empty).
Worked Example
enqueue `1, 2` onto `in=[1,2]`, then on `dequeue()` flip onto `out=[2,1]` and pop `1`—subsequent `enqueue(3)` stays in `in=[3]` while `dequeue()` still pops `2` from `out` in O(1)!
Common Pitfall
forgetting to transfer only when `out_stack` is empty, which breaks FIFO order and turns every call into O(n)!
Recommended Readings & Practice
Practice Queue & Deque with 3 graded MCQs & written feedback.
Take SOARDO’s free 12-question diagnostic to see how strong you are across your target role and unlock adaptive daily missions.