Data Structures & Algorithms
A complete guide for Java developers β from fundamentals to interview patterns. Each topic builds on the last. Work through in order, or jump to what you need.
Memory layout, JVM primitives vs references, and the reasoning framework for choosing structures.
Big O, Big Ξ, Big Ξ© β time and space analysis, amortised cost, and the nββ mental model.
Contiguous memory, O(1) access by index, O(n) insert/delete in the middle.
Node chain with O(1) insert/delete at known positions, O(n) access by index.
LIFO β last in, first out. Push and pop in O(1). Undo, recursion, parsing.
FIFO β first in, first out. Enqueue and dequeue in O(1). BFS, scheduling.
Double-ended queue β O(1) add/remove at both ends. Monotonic queues, sliding window max.
O(1) average keyβvalue lookup. Bucket array + linked list/tree for collisions.
O(1) average membership test. Deduplication, contains-check, and set operations.
At most two children per node. Pre/in/post/level-order traversals in O(n). Recursion backbone.
Left < root < right invariant. O(log n) search/insert/delete when balanced, O(n) worst case.
O(1) min/max peek, O(log n) insert/remove. Top-K problems, merge K lists, stream median.
O(k) lookup by key length, k = word size. Autocomplete, prefix search, word dictionaries.
Vertices, edges, directed/undirected. Adjacency list O(V+E) vs matrix O(VΒ²) representations.
Level-by-level via queue in O(V+E). Shortest path in unweighted graphs, multi-source BFS.
Dive deep then backtrack in O(V+E). Cycle detection, connected components, topological sort.
Linear ordering of a DAG β Kahn's BFS or DFS post-order. Course scheduling, build systems.
Dijkstra O((V+E) log V) for non-negative weights. Bellman-Ford O(VE) for negative edges.
Merge sort, quick sort, heap sort β O(n log n) comparisons and when each excels.
Halve the search space every step β O(log n) on sorted data. Boundaries and search-on-answer.
Opposite-end convergence, read/write compaction, fast/slow cycle detection β O(nΒ²) β O(n).
Fixed or variable window over a sequence β contiguous subarray/substring problems in O(n).
Overlapping subproblems β memoize or tabulate. 1D, 2D, knapsack, string DP patterns.
Choose β explore β unchoose. Subsets, permutations, combinations, N-Queens, Sudoku.
Locally optimal choice at each step. Interval scheduling, jump game, activity selection.
Interview-critical sections (if short on time): Array, HashMap, Binary Tree, BFS/DFS, Binary Search, Two Pointers, Sliding Window, DP, and the Pattern Cheat Sheet.