Aptitude · Tech basics
Data structures and Big O, made easy
💡 The idea
Pick the structure by how you need to get things out: last-in-first-out (stack), first-in-first-out (queue), by key (hash table), in order (sorted array or tree), or by connections (graph).
How do I take items out?→Pick the structure→How fast? (Big O)
🧭 The method: 3 steps
- Stack = pile of plates (LIFO). Queue = line at a counter (FIFO).
- Hash table = instant lookup by key. Graph = things connected to things.
- Big O = how the work grows as n grows: O(1) < O(log n) < O(n) < O(n log n) < O(n²).
🎬 Every kind of question, in pictures
Pick a kind. Guess the answer first, then watch the picture work it out. Tap any step to see it in the picture.
Loading the pictures…
✏️ Worked examples
Example 1. push 1, push 2, pop, push 3. What is on top?
- [1] → [1, 2] → pop → [1] → [1, 3]
⚡ Shortcut: 3
⚠️ Where people go wrong
- Mixing up stack (last in, first out) and queue (first in, first out).
- Binary search only works on SORTED data.
- O(log n) is faster than O(n), not slower.
📌 Remember
- Stack LIFO, queue FIFO
- Hash table: O(1) lookup
- Binary search: log₂ n steps