1.1 · Complexity & Big-O
| Class | Name | Example |
|---|---|---|
| $O(1)$ | Constant | Hashmap get, array index. |
| $O(\log n)$ | Logarithmic | Binary search, balanced BST lookup. |
| $O(n)$ | Linear | One pass over a list. |
| $O(n \log n)$ | Linearithmic | Merge sort, quicksort average. |
| $O(n^2)$ | Quadratic | Naive nested loops. |
| $O(2^n)$ | Exponential | Recursive Fibonacci, brute-force subset. |
| $O(n!)$ | Factorial | Brute-force TSP. |
Space complexity matters the same way. Amortised complexity matters too: a dynamic array's append is $O(1)$ amortised even though some appends trigger $O(n)$ resize.
1.2 · Data structures — the essential set
| Structure | Lookup | Insert | Use when |
|---|---|---|---|
| Array / dynamic array | $O(1)$ by index | $O(1)$ amortised at end | Order matters, indices known. |
| Linked list | $O(n)$ | $O(1)$ at known node | Frequent middle inserts; cache-unfriendly. |
| Hashmap / dict | $O(1)$ avg | $O(1)$ avg | Key→value lookups. |
| Tree map / sorted map | $O(\log n)$ | $O(\log n)$ | Sorted iteration, range queries. |
| Heap / priority queue | $O(\log n)$ pop | $O(\log n)$ | Always need min/max next. |
| Trie | $O(k)$ in key length | $O(k)$ | Autocomplete, prefix matching. |
| Graph (adj list) | $O(V+E)$ traversal | $O(1)$ edge add | Relationships, paths. |
| Bloom filter | $O(k)$ probe | $O(k)$ | Compact "definitely not / maybe" set. |
| HyperLogLog | $O(1)$ approximate count | $O(1)$ | Distinct counts on streams. |
| Skip list | $O(\log n)$ | $O(\log n)$ | Sorted maps with simpler concurrency than BSTs (Redis). |
| B-tree / B+ tree | $O(\log n)$ | $O(\log n)$ | Disk-based indexes (every relational DB). |
| LSM tree | fast writes, slower reads | $O(1)$ append | Write-heavy KV (RocksDB, Cassandra). |
| Roaring bitmap | $O(1)$ bit ops | compact membership at huge cardinalities | Search engines, analytics. |
1.3 · Algorithm families to recognise on sight
| Family | Pattern | Used in |
|---|---|---|
| Sorting | Quicksort, mergesort, timsort, radix. | Every language stdlib. |
| Searching | Linear, binary, interpolation. | Indices, autocomplete. |
| Graph traversal | BFS, DFS. | Web crawl, dependency walk. |
| Shortest path | Dijkstra, A*, Bellman-Ford. | Maps, routing, planners. |
| Dynamic programming | Memoise overlapping sub-problems. | Edit distance, knapsack, DP-on-trees. |
| Greedy | Local-best choice → global solution (sometimes). | Scheduling, Huffman coding. |
| Divide & conquer | Split, solve, combine. | FFT, mergesort. |
| Sliding window / two pointers | O(n) string / array scans. | Substring search. |
| Backtracking | Build, test, rewind. | Solvers, constraint problems. |
| Union-Find / DSU | $O(\alpha(n))$ union/find. | Connectivity, Kruskal's MST. |
| Topological sort | DAG ordering. | Build systems, task schedulers. |
| Reservoir sampling | Uniform sample from a stream of unknown size. | Online analytics. |
| Bit manipulation | $x \& (x-1)$, popcount, XOR tricks. | Compression, hashing, cryptography. |
1.4 · Latency numbers every engineer should know
| Operation | Order |
|---|---|
| L1 cache reference | 0.5 ns |
| Branch mispredict | 5 ns |
| L2 cache reference | 7 ns |
| Mutex lock/unlock | 25 ns |
| Main memory reference | 100 ns |
| Compress 1 KB (snappy) | ~3 μs |
| 1 Gbps network: send 1 KB | ~10 μs |
| SSD random read | ~150 μs |
| Read 1 MB sequentially from SSD | ~1 ms |
| Round trip in same data centre | ~0.5 ms |
| HDD seek | ~10 ms |
| Round trip across continents | ~150 ms |
1.5 · Processes & threads
- Process: isolated virtual address space, file descriptors, resources.
- Thread: independent execution within a process; shares heap, separate stack.
- Context switch: ~1–10 μs; far higher cost if it flushes cache or TLB.
- Fork / exec: Unix model — fork copies (COW), exec replaces.
- Green threads / fibers / goroutines: many user-space threads multiplexed on few OS threads.
1.6 · Memory model
| Segment | What lives there |
|---|---|
| Text / code | Read-only executable instructions. |
| Data / BSS | Initialised and zero-initialised globals. |
| Heap | Dynamic allocations; grows up. |
| Memory-mapped regions | mmap'd files, shared memory. |
| Stack | Function frames; grows down; one per thread. |
- Virtual memory — every process sees a private address space; the MMU maps pages.
- Pages — typically 4 KB; huge pages (2 MB / 1 GB) for DBs / JVMs.
- Page fault — first touch loads from disk or zeros the page; major faults hit disk.
- Copy-on-write — fork() shares pages until either side writes.
- OOM killer — kernel reaps a process under memory pressure (configure with
oom_score_adj).
1.7 · Files & I/O
| I/O model | Definition | Used in |
|---|---|---|
| Blocking | Call stops until I/O completes. | Simple scripts. |
| Non-blocking | Call returns immediately with WOULDBLOCK. | Event loops. |
| I/O multiplexing | One thread watches many FDs (select/poll/epoll/kqueue). | Nginx, Node, Redis. |
| Async I/O (io_uring, IOCP) | Kernel completes work; userland polls a ring. | Modern Linux services, Windows. |
| Memory-mapped (mmap) | File as virtual memory; OS pages in. | Databases, large reads. |
1.8 · System calls you should recognise
| Syscall | What it does |
|---|---|
open / openat | Acquire a file descriptor. |
read / write / pread / pwrite | Bytes in/out. |
mmap / munmap | Map a file (or anon memory). |
fork / clone / execve | Create / replace processes. |
epoll_create / epoll_ctl / epoll_wait | Event-loop primitives. |
io_uring | High-perf async I/O on Linux. |
socket / bind / listen / accept / connect | BSD sockets. |
kill / sigaction | Signals. |
futex | Fast user-space mutex backing. |
1.9 · Linux essentials a tech-savvy person never relearns
| Topic | Tools |
|---|---|
| Processes / signals | ps, top, htop, kill, strace, lsof. |
| I/O & disk | iostat, iotop, du, df, fio, blktrace. |
| Network | ss, tcpdump, wireshark, mtr, dig, ip. |
| Performance | perf, bpftrace, eBPF, flamegraph. |
| Filesystems | ext4, XFS, ZFS, btrfs. |
| Init / services | systemd, journalctl, units, sockets. |
| Cgroups / namespaces | The two primitives Docker is built on. |
1.10 · Self-check
Q1 · Hash-map worst-case lookup is…
$O(1)$
$O(n)$
$O(\log n)$
$O(n \log n)$