Practice Hub
Data Structures & Algorithms
Build muscle memory for common coding patterns. Rehearse sliding windows, two pointers, tree traversals, and sorting algorithms step-by-step with interactive trace arrays.
Two Pointers
Two indices scanning toward each other or at different speeds
- O(n) Complexity
- Space: O(1)
Sliding Window
Maintain a window of elements — expand right, shrink left
- O(n) Complexity
- Space: O(1)
Prefix Sum
Pre-compute cumulative sums for O(1) range queries
- O(n) build, O(1) query Complexity
- Space: O(N)
Kadane's Algorithm
Maximum subarray sum — keep running total or restart
- O(n) Complexity
- Space: O(1)
Cyclic Sort
Place each number at its correct index in O(n)
- O(n) Complexity
- Space: O(1)
Boyer-Moore Voting
Cancel opposing votes — the majority survives
- O(n) Complexity
- Space: O(1)
Dutch National Flag
Three-way partition with low / mid / high pointers
- O(n) Complexity
- Space: O(1)
Binary Search
Halve the search space every step — O(log n) on sorted data
- O(log n) Complexity
- Space: O(1)
Binary Search on Answer
Binary search over the answer space, not the array
- O(n log(max-min)) Complexity
- Space: O(1)
Monotonic Stack
Stack that stays sorted — find next greater/smaller in O(n)
- O(n) Complexity
- Space: O(N)
Monotonic Queue
Deque that keeps window max/min reachable in O(1)
- O(n) Complexity
- Space: O(K)
Balanced Parentheses
Use a stack to match opening and closing brackets
- O(n) Complexity
- Space: O(N)
Fast & Slow Pointers
Floyd's cycle detection — slow moves 1 step, fast moves 2
- O(n) Complexity
- Space: O(1)
LinkedList Reversal
In-place reversal with three pointers: prev / curr / next
- O(n) Complexity
- Space: O(1)
K-way Merge
Min-heap tracks the front of K sorted lists
- O(n log k) Complexity
- Space: O(K)
BFS Level Order
Queue processes the tree level by level
- O(n) Complexity
- Space: O(W)
DFS Traversal
Recurse into children — pre / in / post order
- O(n) Complexity
- Space: O(H)
Tree DP
Compute each subtree's answer bottom-up
- O(n) Complexity
- Space: O(H)
Lowest Common Ancestor
Deepest node that is an ancestor of both targets
- O(n) Complexity
- Space: O(H)
Trie (Prefix Tree)
Tree of characters for fast prefix lookups
- O(L) per op Complexity
- Space: O(N·L)
BFS/DFS Islands
Flood-fill connected components in a grid
- O(rows·cols) Complexity
- Space: O(ROWS·COLS)
Topological Sort
Order a DAG so every edge points forward
- O(V+E) Complexity
- Space: O(V+E)
Union-Find (DSU)
Track connected components with path compression
- O(α(n)) per op Complexity
- Space: O(N)
Dijkstra's Shortest Path
Greedy shortest paths with a min-heap
- O(E log V) Complexity
- Space: O(V)
Bellman-Ford
Relax all edges V-1 times — handles negative weights
- O(V·E) Complexity
- Space: O(V)
Multi-source BFS
Start BFS from all sources at once
- O(rows·cols) Complexity
- Space: O(ROWS·COLS)
1D DP (Fibonacci)
Current state depends on the last 1–2 states
- O(n) Complexity
- Space: O(1)
0/1 Knapsack
Include or exclude each item once
- O(n·W) Complexity
- Space: O(W)
Unbounded Knapsack
Each item can be picked unlimited times
- O(n·W) Complexity
- Space: O(W)
Longest Common Subsequence
2D table — characters match or skip one
- O(m·n) Complexity
- Space: O(M·N)
Longest Increasing Subsequence
Patience sorting — binary search the tails
- O(n log n) Complexity
- Space: O(N)
Matrix / Grid DP
Fill a grid from top-left to bottom-right
- O(m·n) Complexity
- Space: O(N)
Interval DP
Solve ranges by trying every split point
- O(n³) Complexity
- Space: O(N²)
State Machine DP
Track distinct states that transition on decisions
- O(n·k) Complexity
- Space: O(K)
Top K Elements
A size-K heap keeps the best K seen so far
- O(n log k) Complexity
- Space: O(K)
Two Heaps (Median)
Max-heap (low half) + min-heap (high half)
- O(log n) add Complexity
- Space: O(N)
K-way Merge (Heap)
Heap of K list-fronts merges sorted sources
- O(n log k) Complexity
- Space: O(K)
Subsets / Combinations
At each index: choose it or skip it
- O(2ⁿ) Complexity
- Space: O(N)
Permutations
Swap, recurse, swap back
- O(n!) Complexity
- Space: O(N)
Constraint Satisfaction
Place, validate, prune invalid branches
- O(exp) Complexity
- Space: O(N)
Interval Scheduling
Sort by end time, greedily take the earliest finish
- O(n log n) Complexity
- Space: O(1)
Jump Game
Track the farthest index reachable so far
- O(n) Complexity
- Space: O(1)

