Practice problems
Original problems for each pattern, with a three-step hint ladder, the full solution and follow-up questions. Try before you peek.
- EasyHash maps
Spot the repeat
Given a list of numbers, say whether any number appears twice.
- EasyHash maps
Pair that adds up
In an unsorted array, find the indices of two numbers that add up to a target.
- EasyFrequency counting
Same letters?
Say whether two strings use exactly the same letters the same number of times.
- MediumPrefix sums
Everything but me
For each position, return the product of all the other numbers, without dividing.
- MediumPrefix sums
Slices that add to k
Count the contiguous slices of an array (negatives allowed) whose sum is exactly k.
- EasyTwo pointers
Reads the same both ways
Say whether a sentence is a palindrome, ignoring case and anything that isn't a letter or digit.
- MediumTwo pointers
The fullest container
Pick two vertical bars so that the water held between them is as large as possible.
- MediumTwo pointers
Three that cancel out
List every unique triple of numbers that adds up to zero.
- EasySliding window
Best stretch of k
Find the contiguous stretch of k numbers with the highest average.
- MediumSliding window
The longest run without repeats
Find the length of the longest substring that has no repeated character.
- EasyBinary search
Where does it go?
In a sorted array, find where a value is, or where it should be inserted to keep the order.
- MediumSearch on the answer
The slowest speed that finishes
Find the minimum eating speed that finishes all the piles within h hours.
- EasyLinked lists
Flip the chain
Reverse a singly linked list and return the new head.
- EasyStacks & queues
Do the brackets match?
Check that every opening bracket is closed by the right type, in the right order.
- MediumMonotonic stack
How long until it warms up?
For each day, find how many days you must wait for a warmer temperature.
- MediumIntervals
Merge the overlapping ranges
Merge all overlapping intervals and return the disjoint result.
- EasyTraversals
How tall is the tree?
Return the number of nodes on the longest root-to-leaf path of a binary tree.
- MediumTraversals
The tree, level by level
Return the values of a binary tree grouped by level, top to bottom.
- MediumBinary search trees
Is it really a search tree?
Decide whether a binary tree satisfies the binary search tree rule at every node.
- MediumTries
An index of word prefixes
Design a structure that stores words and answers whether any stored word starts with a prefix.
- MediumPriority queues
The k-th biggest number
Find the k-th largest element of an unsorted array.
- MediumTop-K
The k most common values
Return the k values that appear most often in an array.
- MediumBFS & DFS
How many islands?
Count the groups of connected land cells in a grid of land and water.
- MediumTopological sort
Can you finish every course?
Given course prerequisites, decide whether all courses can be completed.
- MediumShortest paths
How long until everyone gets the signal?
Find the time for a signal to reach every node of a weighted directed network.
- MediumUnion-find
Friend circles
Count the groups of people connected directly or indirectly.
- MediumUnion-find
The cable that makes a loop
Find the edge whose addition turns a tree into a graph with a cycle.
- MediumSubsets & combinations
Every possible team
Return all subsets of a list of distinct numbers.
- MediumSubsets & combinations
Ways to hit the target
Find every combination of candidates (reusable) that adds up to a target.
- MediumPermutations
Every seating order
Return all permutations of a list of distinct numbers.
- Easy1D dynamic programming
Ways up the stairs
Count the ways to climb n stairs taking 1 or 2 steps at a time.
- Medium1D dynamic programming
Fewest coins
Find the fewest coins that add up to an amount, or −1 if impossible.
- Medium1D dynamic programming
Rob the street without alarms
Max sum of non-adjacent elements.
- Medium2D dynamic programming
Letters in common, in order
Length of the longest common subsequence of two strings.
- Medium2D dynamic programming
Robot on a grid
Count paths from top-left to bottom-right moving right or down, avoiding obstacles.
- MediumKnapsack
Best loot for the bag
Maximise total value of items that fit in a bag, each used at most once.
- MediumKnapsack
Two piles, same total
Can the numbers be split into two groups with equal sum?
- MediumGreedy
Can you reach the last cell?
Each cell says the max jump length; can you reach the end?
- MediumGreedy
Most meetings in one room
Pick the largest set of non-overlapping meetings.
- EasyBit manipulation
The odd one out
Find the number that appears once when all others appear twice, in O(1) space.
- EasyBit manipulation
Count the lit bits
Count the 1 bits in the binary form of a non-negative integer.
- MediumPrimes, GCD & fast power
How many primes?
Count the primes strictly less than n.