Stage 1 · Code
Stacks & Queues
Stack
LIFO operations, array vs linked list implementation.
5 min readMastering Data Structures & Algorithms for Software Engineering InterviewsCode
Stack Concept
Stack — Last In, First Out (LIFO)
Stack
toptop
middle
bottom
Stack — push and pop operations animated
Stack (LIFO)
empty
Step 1 / 8 — Empty stack
A stack is a LIFO (Last In, First Out) data structure. Think of a stack of plates — you can only add/remove from the top. Fundamental operations: push (add), pop (remove top), peek (view top).
Core Operations
| Operation | Description | Time |
|---|---|---|
| Push | Add element to top | O(1) |
| Pop | Remove and return top | O(1) |
| Peek/Top | View top without removing | O(1) |
| IsEmpty | Check if stack empty | O(1) |
| Size | Number of elements | O(1) |
Go Implementation
Gostack-with-slice-(preferred-in-go).go
32 linesLn 1, Col 1Go
Go slices are ideal for stacks. Append = push, slice truncate = pop. Both O(1) amortized. Cache-friendly due to contiguous memory.
Applications
- Call stack: Function execution tracking.
- Expression evaluation: Postfix/prefix calculators.
- Undo operations: Text editors, browser history.
- Backtracking: Maze solving, N-Queens.
- Balanced parentheses: Compiler syntax checking.
- Next greater element: Monotonic stack pattern.
Mark this lesson complete to store local progress and unlock a cleaner resume path the next time you visit.