DSA Workbook

539 problems grouped by algorithmic pattern. Each entry has a paraphrased problem statement, examples, hints, a C++ solution, an editorial walk-through, and a pattern-revision callout.

138 Easy 258 Medium 143 Hard 29 patterns RSS

Two Pointers

27 problems

Converging or parallel pointers scan arrays and strings in linear time, replacing nested O(n²) loops with single O(n) sweeps. Most useful when the data is sorted or has a symmetric structure (palindromes, pair sums).

Floyd's tortoise-and-hare. Speed-2 vs speed-1 pointers detect cycles, find midpoints, and solve linked-list-as-functional-graph problems in O(1) extra space. The atomic cycle-detection primitive.

Sliding Window

19 problems

Maintain a contiguous window over an array or string, expanding and shrinking to preserve an invariant. Powers most 'longest / shortest / count substring with property X' problems, often turning O(n²) brute force into O(n).

Intervals

11 problems

Sort intervals by start, then sweep linearly. Merge, intersect, count overlaps, and detect meeting-room collisions all fall out of a single linear pass. A min-heap of end times adds resource-allocation flavors on top.

In-Place Linked List

14 problems

Reverse, reorder, rotate, and partition linked lists by rewiring `next` pointers — O(1) space. Building blocks: dummy head, three-pointer reversal, head-insertion within a sublist, and the split-reverse-weave composition.

Heaps

12 problems

Priority queues answer 'what's the smallest or largest right now?'. Use bounded heaps for top-k, dual heaps for running medians, and lazy deletion for sliding-window extrema.

K-way Merge

7 problems

Merge k sorted streams via a min-heap of frontiers — one entry per stream. The foundation for kth-smallest across multiple sources, sorted-matrix queries, and external sorting.

Top K Elements

18 problems

Bounded heap of size k: min-heap for top-k-largest, max-heap for top-k-smallest. When one-shot O(n) average is acceptable, quickselect partitions around a pivot instead.

Subsets

8 problems

DFS enumeration of subsets, permutations, and combinations. Start-index for combinations; in-place swap for permutations; sort-and-skip for dedup on duplicate inputs.

Greedy Techniques

22 problems

Take the locally-best move and prove (via exchange argument or matroid) that it leads to a globally optimal solution. Sorts, heaps, and running aggregates are the usual scaffolding.

Backtracking

20 problems

Recursive search with pruning. Build candidate solutions incrementally and abort branches that violate constraints early. Bitmasks compress constraint state when feasible.

Dynamic Programming

38 problems

Express the answer as overlapping subproblems with optimal substructure; cache results. Flavors range from 1D rolling DP through 2D knapsack, interval DP, state machines, and bitmask DP.

Cyclic Sort

6 problems

Place value `v` at index `v - 1` via in-place swaps. The canonical O(n) / O(1) primitive for missing-or-duplicate problems on arrays containing the integers `[1, n]`.

Topological Sort

11 problems

Linear ordering of a DAG respecting dependencies. Kahn's BFS for indegree-based ordering; DFS post-order for the reverse. Cycle detection comes for free.

Matrices

18 problems

Treat the 2D grid as the data structure. In-place markers, layer-by-layer iteration, spiral traversal, transpose-then-reverse rotation, prefix sums, and staircase walks.

Stacks

17 problems

LIFO order captures 'last-seen-first-out' semantics. Monotonic stacks find next-greater or next-smaller in O(n); expression evaluation and nested-structure validation are direct applications.

Graphs

17 problems

Encode relations as edges. BFS for shortest unweighted paths, DFS for connectivity and topological order, Dijkstra / Bellman-Ford for weighted shortest paths, A* when a heuristic is available.

Tree DFS

21 problems

Recursive depth-first traversal. Pre / in / post-order, path-sum aggregation, LCA, height, diameter, serialization, BST validation — most tree questions reduce to a single DFS with carefully chosen return values.

Tree BFS

13 problems

Level-order traversal via a queue. Right-side view, zigzag, per-level aggregation, connecting next-right pointers, and shortest path in unweighted trees.

Trie

15 problems

Prefix tree for word lookups, autocomplete, prefix counting, and word-search-in-grid. Each node has up to |Σ| children; operations are O(L) in the word length.

Hash Maps

29 problems

Constant-time keyed lookup. Frequency counting, complement search, anagram grouping, two-sum, and running aggregations keyed by computed signatures — the most reused primitive in the entire toolkit.

Tracking Patterns

18 problems

Run a rolling aggregation (sum, count, last-seen index) over a stream and test it against a target. A cousin of hash maps that emphasizes mutable state over time.

Union Find

14 problems

Disjoint-set forest with path compression and union by rank. Maintain connected components dynamically as edges arrive in near-O(1) per op (inverse Ackermann).

Custom Data Structs

16 problems

Compose primitives — hash + linked list, twin heaps, segment trees, balanced BSTs — to satisfy an unusual operation spec in optimal time. LRU, LFU, time-keyed stores, snapshot arrays.

Bitwise Operations

17 problems

Each bit is a flag or part of an integer encoding. XOR cancels pairs, AND masks, popcount counts set bits. Subset masks, parity tricks, position toggles, and arithmetic without operators.

Math & Geometry

37 problems

Number theory (GCD, modular arithmetic, primes), big-number arithmetic on strings, coordinate geometry, area and orientation tests, and modular exponentiation.

Review Challenges

46 problems

A grab-bag of foundational problems that appear in interviews regardless of pattern. Two Sum, Maximum Subarray, Trapping Rain Water — the canonical warm-ups.

Search ESC

Keyboard shortcuts

Shortcuts are disabled while typing in inputs.