Solved problems

50 original problems so far. Each has hints you reveal one at a time, the reasoning behind the solution, why it is correct, what it costs, the same solution in four languages, and a walkthrough you can run on your own inputs. Nothing is graded: there is no submission and no verdict.

ProblemDifficultyTechniqueTime
Two prices that fit the budget exactlybeginnertwo pointers, sorted arrayO(n)
Two readings that add up to a targetbeginnerhashing, arraysO(n) expected
Has any badge been scanned twice?beginnerhashing, setsO(n) expected
Push the empty slots to the endbeginnertwo pointers, read/write pointersO(n)
Do the brackets close properly?beginnerstacks, stringsO(n)
The busiest stretch of k hoursbeginnersliding window, arraysO(n)
The best streak of daily profitsintermediatedynamic programming, Kadane's algorithmO(n)
Every product but your ownintermediateprefix products, arraysO(n)
The longest stretch without a repeated letterintermediatesliding window, hashingO(n) expected
Where does a value start and end?intermediatebinary search, lower boundO(log n)
The one ticket that never came backbeginnermath, arraysO(n)
Does the phrase read the same both ways?beginnertwo pointers, stringsO(n)
Same letters, different order?beginnerhashing, countingO(n) expected
Where do both sides weigh the same?beginnerprefix sums, arraysO(n)
Two walls that hold the most waterintermediatetwo pointers, greedyO(n)
Shift the schedule by k daysintermediatearrays, in placeO(n)
Red, white and blue in one passintermediatetwo pointers, partitionO(n)
Which candidate has more than half the votes?intermediatevoting algorithm, arraysO(n)
How many days until it gets warmer?intermediatemonotonic stack, stacksO(n)
How many stretches add up to k?intermediateprefix sums, hashingO(n) expected
Buy once, sell once, profit mostbeginnerarrays, greedyO(n)
Keep one copy of each sorted valuebeginnertwo pointers, read/write pointersO(n)
Merge two sorted lists into the first onebeginnertwo pointers, mergingO(m + n)
Find the first broken buildbeginnerbinary search, monotonic predicateO(log n) checks
The sock without a pairbeginnerbit manipulation, XORO(n)
Count the 1 bitsbeginnerbit manipulation, masksO(number of 1 bits) ≤ O(width)
The largest tile that fits both wallsbeginnermath, Euclidean algorithmO(log min(a, b)) iterations
Run-length encode a wordbeginnerstrings, two pointersO(n)
How many ways up the stairs?beginnerdynamic programming, tabulationO(n)
Collect the most without taking neighboursintermediatedynamic programming, state machineO(n)
The first letter that never repeatsbeginnerstrings, frequency countO(n)
Group words that share their lettersintermediatestrings, hashingO(n · L log L) expected
Count the mirror-image substringsintermediatestrings, palindromesO(n²) worst case
Set bits for every number up to nbeginnerbit manipulation, dynamic programmingO(n)
The one value not in a tripleadvancedbit manipulation, XORO(n)
Can you hop to the last stone?intermediategreedy, arraysO(n)
Where to start a round trip on a fuel budgetintermediategreedy, circular arrayO(n)
Next bigger value on a ringintermediatemonotonic stack, circular arrayO(n)
Where does the linked list loop back?advancedlinked lists, fast and slow pointersO(n)
The cheapest walk down and rightintermediatedynamic programming, gridO(rows · columns)
The k-th highest scorebeginnerheap, top-kO(n log k)
The k values seen most oftenintermediateheap, top-kO(n + m log k)
Combine overlapping bookingsintermediateintervals, sortingO(n log n)
How many rooms for these meetings?intermediateintervals, sortingO(n log n)
Fewest steps across a walled gridintermediateBFS, gridO(rows · columns)
Minutes until the whole office knowsintermediateBFS, multi-source BFSO(rows · columns)
Count the separate land massesintermediateDFS, gridO(rows · columns)
How many separate networks?intermediateunion-find, disjoint setsO(n + m · α(n))
The first cable that closes a loopintermediateunion-find, cycle detectionO(n + m · α(n))
An order that respects every dependencyintermediatetopological sort, Kahn's algorithmO(n + m)

Techniques covered: BFS · DFS · Dutch national flag · Euclidean algorithm · Fibonacci · Kadane's algorithm · Kahn's algorithm · XOR · anagrams · arithmetic series · arrays · binary search · bit manipulation · canonical key · circular array · complement · connected components · counting · counting mod 3 · cycle detection · disjoint sets · dynamic programming · expand around center · fast and slow pointers · flood fill · frequency count · gcd · graphs · greedy · grid · hashing · heap · in place · intervals · level order · linked lists · lower bound · masks · math · merging · monotonic predicate · monotonic stack · multi-source BFS · next greater element · palindromes · parsing · partition · prefix products · prefix sums · priority queue · queue · reachability · read/write pointers · reversal · running minimum · running sum · search on answer · sets · shifts · shortest path · sliding window · sorted array · sorting · stack · stacks · state machine · string builder · strings · sweep · sweep line · tabulation · top-k · topological sort · two pointers · union-find · voting algorithm.