Both are linear data structures that control the order in which elements are removed, but in opposite ways. Stack — LIFO (Last In, First Out) - The last element added is the first one removed, like a stack of plates. - Main operations, all O(1): - Push: add to the top. - Pop: remove from the top. - Peek (top): read the top without removing it. - Typical uses: - Function call stack (each call is pushed; returning pops it). - Undo and redo features. - Browser back navigation. - Expression evaluation and syntax parsing (balanced parentheses, converting infix to postfix). - Depth-first search (DFS) and backtracking algorithms. Queue — FIFO (First In, First Out) - The first element added is the first one removed, like a line at a ticket counter. - Main operations, all O(1): - Enqueue: add to the back. - Dequeue: remove from the front. - Peek (front): read the front without removing it. - Typical uses: - Task scheduling and print queues. - Breadth-first search (BFS). - Buffering data between producers and consumers. - Message queues between services (Azure Service Bus, RabbitMQ, Kafka-style processing). - Request handling in web servers. Code examples (C#) var stack = new Stack<int>(); stack.Push(1); stack.Push(2); stack.Push(3); Console.WriteLine(stack.Pop()); // 3 (last in, first out) var queue = new Queue<int>(); queue.Enqueue(1); queue.Enqueue(2); queue.Enqueue(3); Console.WriteLine(queue.Dequeue()); // 1 (first in, first out) Java equivalents: Deque<Integer> stack = new ArrayDeque<>(); stack.push(x); stack.pop(); and Queue<Integer> q = new ArrayDeque<>(); q.offer(x); q.poll(); (ArrayDeque is preferred over the legacy Stack class in Java.) Classic interview problem using a stack: valid parentheses bool IsBalanced(string s) { var pairs = new Dictionary<char, char> { [')'] = '(', [']'] = '[', ['}'] = '{' }; var stack = new Stack<char>(); foreach (var c in s) { if (c is '(' or '[' or '{') stack.Push(c); else if (pairs.TryGetValue(c, out var open)) { if (stack.Count == 0 || stack.Pop() != open) return false; } } return stack.Count == 0; } IsBalanced("{[()]}") returns true; IsBalanced("([)]") returns false. Variations - Deque (double-ended queue): insert and remove at both ends (LinkedList<T> or ArrayDeque). - Priority queue: elements removed by priority rather than insertion order (PriorityQueue<TElement, TPriority> in .NET 6+, PriorityQueue in Java), implemented as a binary heap with O(log n) operations — used in Dijkstra's algorithm and job scheduling. - Circular queue: fixed-size array where the front and rear wrap around, avoiding shifting. - Concurrent versions for multithreaded code: ConcurrentStack<T>, ConcurrentQueue<T>, and Channel<T> for producer-consumer pipelines in .NET. Implementation notes - Both can be implemented with arrays (dynamic arrays with an index pointer) or linked lists. - Implementing a queue using two stacks is a classic exercise: push to an input stack, and when dequeuing, move elements to an output stack if it is empty — amortised O(1). Interview summary: "A stack is LIFO — push and pop at the top, used for function calls, undo and DFS. A queue is FIFO — enqueue at the back and dequeue from the front, used for scheduling, buffering and BFS. Both offer O(1) core operations."
Ravindra Wadile · · 0 likes