Stack and queue — how do they differ and where are they used?
The gist
- Stack — LIFO: push and pop at one end (
push/pop). Uses: the call stack, undo, depth-first traversal (DFS), bracket matching, expression parsing, the navigation stack (NavigationStack,UINavigationController). - Queue — FIFO: enqueue at the tail, dequeue from the head. Uses: task queues (
DispatchQueue,OperationQueue), breadth-first traversal (BFS), event buffers. - Deque — O(1) operations at both ends; not in the stdlib, but
Dequeexists in swift-collections.
The rest of this answer, with code and interview follow-ups, is in the DevPrep app.
Read the full answer in DevPrep