Algorithm practice problems
Laddered easy, medium and hard coding problems for every algorithms topic, graded in your browser against hidden tests. Hints and Python + C++ solutions.
Arrays & strings
- Two Sum (Easy) — Find the two indices whose values add up to a target.
- Running Sum (Easy) — Build the array of prefix sums.
- Move Zeroes (Easy) — Push all zeroes to the end, keeping the order of the rest.
- Valid Palindrome (Easy) — Ignore case and non-alphanumerics — is it a palindrome?
- Maximum Subarray (Medium) — Largest sum of any contiguous subarray (Kadane's algorithm).
- Product of Array Except Self (Medium) — result[i] = product of every element except nums[i] — no division.
- Longest Substring Without Repeating (Medium) — Sliding window over a string with a last-seen map.
- Trapping Rain Water (Hard) — How much water is trapped between the bars after it rains?
- Group Anagrams (Hard) — Bucket words by their sorted-letter signature.
Searching
- Binary Search (Easy) — Find a target in a sorted array in O(log n).
- Search Insert Position (Easy) — Where would the target go in a sorted array?
- Search in Rotated Sorted Array (Medium) — Binary search in a sorted array that was rotated at an unknown pivot.
- Median of Two Sorted Arrays (Hard) — Find the median of two sorted arrays as a single value.
Recursion
- Factorial (Easy) — Compute n! with a base case and a recursive step.
- Fibonacci Number (Easy) — Return the n-th Fibonacci number (0-indexed).
- Sum of Digits (Easy) — Add up the decimal digits of a non-negative integer.
- Generate Parentheses (Medium) — All well-formed combinations of n pairs of parentheses.
- Permutations (Hard) — Every ordering of a list of distinct integers.
Hashing
- Contains Duplicate (Easy) — Does any value appear at least twice?
- Valid Anagram (Easy) — Is t a rearrangement of the letters of s?
- First Unique Character (Easy) — Index of the first non-repeating character.
- Top K Frequent Elements (Medium) — Return the k most frequent values (any order).
- Longest Consecutive Sequence (Hard) — Length of the longest run of consecutive integers — in O(n).
Stacks & queues
- Valid Parentheses (Easy) — Are the brackets balanced and correctly nested?
- Reverse String (Easy) — Return the characters of a string in reverse order.
- Daily Temperatures (Medium) — Days until a warmer temperature — a monotonic stack.
- Largest Rectangle in Histogram (Hard) — Biggest axis-aligned rectangle fitting under a histogram.
Sorting
- Merge Two Sorted Arrays (Easy) — The merge step at the heart of merge sort.
- Kth Largest Element (Medium) — Find the k-th largest value (1-indexed) in an unsorted array.
- Count Inversions (Hard) — How far is the array from sorted? Count out-of-order pairs in O(n log n).
Trees
- BST Search Path (Easy) — Trace the nodes visited while searching a BST.
- Kth Smallest in a BST (Medium) — An in-order walk of a BST yields sorted order.
- Lowest Common Ancestor in a BST (Hard) — Use the BST ordering to find the split point of two values.
Traversals
- Inorder Traversal (Easy) — Visit left, node, right — return the value sequence.
- Level-Order Traversal (Medium) — Group node values by depth using a BFS queue.
- Zigzag Level-Order (Hard) — Level-order, but alternate left→right and right→left per level.
Linked lists
- Reverse a Linked List (Easy) — Flip the order of the nodes.
- Remove Nth Node From End (Medium) — Delete the n-th node counting from the tail in one pass.
- Add Two Numbers (Linked Lists) (Hard) — Add two numbers stored as digit lists, least-significant first.
Pathfinding
- Number of Islands (Easy) — Count connected groups of land in a grid.
- Shortest Path in a Grid (Medium) — Fewest cells from top-left to bottom-right (BFS finds it).
- Rotting Oranges (Hard) — Multi-source BFS: how long until every fresh orange rots?
Heaps
- Last Stone Weight (Easy) — Repeatedly smash the two heaviest stones — a max-heap.
- K Smallest Elements (Medium) — Return the k smallest values, sorted ascending.
- Merge K Sorted Lists (Hard) — Merge many sorted lists with a min-heap of their heads.
Dynamic programming
- Climbing Stairs (Easy) — How many ways to climb n stairs taking 1 or 2 steps? (1D DP)
- Coin Change (Medium) — Fewest coins to make an amount (1D DP over amounts).
- Longest Common Subsequence (Hard) — Length of the longest subsequence shared by two strings (2D DP).
- Assignment Problem (Bitmask DP) (Hard) — Assign n workers to n tasks at minimum cost — dp over subsets.
- Travelling Salesman (Held-Karp) (Hard) — Shortest tour visiting every city once — exact, via bitmask DP.
Bit manipulation
- Single Number (Easy) — Every value appears twice except one — find it with XOR.
- Counting Bits (Medium) — Popcount for every number 0..n in O(n) using a bit DP.
Math for algorithms
- Greatest Common Divisor (Easy) — Euclid's algorithm: gcd(a, b) = gcd(b, a mod b).
- Count Primes (Medium) — How many primes are strictly less than n? — the sieve.
- Modular Exponentiation (Medium) — Compute base^exp mod m fast with binary exponentiation.
- Binomial Coefficient mod p (Hard) — C(n, r) mod a prime, using factorials and a modular inverse.
Greedy algorithms
- Activity Selection (Easy) — Max non-overlapping intervals — take earliest finish each time.
- Jump Game (Medium) — Can you reach the last index? Track the furthest reachable point.
- Minimum Arrows to Burst Balloons (Hard) — Fewest points that stab every interval — a greedy sweep.
- Maximum Product Subarray (Hard) — Largest product of a contiguous subarray — track max AND min.
Strings & pattern matching
- Find the Index of First Match (Easy) — Return the first index where `needle` occurs in `haystack`.
- Count Pattern Occurrences (Medium) — How many times does the pattern occur (overlaps allowed)?
- Longest Happy Prefix (Hard) — Longest proper prefix that is also a suffix — the KMP failure value.
Graphs
- Number of Connected Components (Easy) — Count the connected pieces of an undirected graph.
- Course Schedule (Medium) — Can you finish all courses? — detect a cycle in a dependency graph.
- Topological Order (lexicographically smallest) (Hard) — Order a DAG so every edge points forward — smallest order, or none.
Divide & conquer
- Fast Power (xⁿ) (Easy) — Compute xⁿ in O(log n) by squaring.
- Majority Element (Medium) — The element appearing more than n/2 times.
Game theory
- Nim — Who Wins? (Easy) — First player wins iff the XOR of pile sizes is non-zero.
- Subtraction Game (Medium) — Win/Lose DP: can the first player win from n with allowed moves?
- Grundy Number (Sprague–Grundy) (Hard) — Compute the Grundy value of a subtraction-game position.
Minimum spanning trees
- Valid Spanning Tree (Easy) — Check whether undirected edges connect all n nodes without a cycle.
- Minimum Connection Cost (Medium) — Return the MST weight of a weighted undirected graph, or -1 if disconnected.
- Second Best MST (Hard) — Find the cheapest spanning tree whose total weight is strictly larger than the MST.