Practice problems
114 problems
Replace each position with the sum of everything up to it.
Can each kid become the richest if given all the extra candies?
Find the value that fills more than half of the list.
Rebuild a string with its most frequent characters first.
Arrange a deck so a reveal-and-tuck routine shows cards in sorted order.
From a list of match results, find the unbeaten and the once-beaten players.
Find the smallest positive integer that's not in an unsorted list, in O(n) time and O(1) space.
Does any value appear at least twice?
Are two strings made of exactly the same letters?
Find the two indices whose values add up to a target.
Bucket words that are anagrams of each other.
Return the k values that appear most often.
For each index, the product of every other element, without division.
Check a partly filled 9x9 board for repeated digits.
Length of the longest run of consecutive integers, in O(n).
Most points sharing one straight line, using slopes as hash keys.
Check whether a sentence reads the same both ways, ignoring case and punctuation.
Push every zero to the end in place, keeping the other values in order.
Square a sorted list (with negatives) and return the squares in sorted order in O(n).
Find the two positions in a sorted list that add up to a target, using O(1) space.
Find every unique triplet that sums to zero.
Pick two vertical lines that, with the x-axis, hold the most water.
Compute how much rain water collects between bars of an elevation map.
Find the biggest gain from buying on one day and selling on a later day.
Largest average of any k consecutive numbers.
Length of the longest stretch of a string with no repeated character.
Longest run of one letter after changing at most k characters.
Does some rearrangement of s1 appear as a substring of s2?
Shortest substring of s that contains every character of t.
Maximum of every window of size k, in O(n).
Answer many range-sum queries on a fixed array in O(1) each.
Find the first index where the sum on the left equals the sum on the right.
Count contiguous subarrays whose sum is exactly k, negatives allowed.
Longest subarray with equally many 0s and 1s.
Sum any sub-rectangle of a fixed grid in O(1).
Apply many range additions to flight seat counts with a difference array.
Shortest subarray with sum ≥ k when negatives are allowed: prefix sums + monotonic deque.
Check that every bracket is closed by the right type, in the right order.
Look up each query's next greater element in another array.
Design a stack that also returns its minimum in O(1).
Compute the value of a postfix arithmetic expression.
For each day, count the days until a warmer one.
Count the groups of cars that reach the destination together.
Find the biggest rectangle that fits under a bar chart.
Find a target's index in a sorted array in O(log n).
Return where a target is, or where it would be inserted, in a sorted array.
Search a row-sorted matrix whose rows continue each other, in O(log(mn)).
Find the slowest eating speed that finishes all piles in h hours.
Find the smallest value of a rotated sorted array in O(log n).
Find a target's index in a rotated sorted array in O(log n).
Split an array into k parts so the biggest part sum is as small as possible.
Flip every next pointer so the list runs backwards.
Splice two sorted lists into one sorted list.
Return the middle node (the second one when there are two) in one pass.
Delete the node n places from the end, in one pass.
Weave the list as first, last, second, second-to-last, ... in place.
Add two numbers stored as reversed digit lists, carrying as you go.
A fixed-size cache with O(1) get/put that evicts the least recently used key.
Reverse every block of k nodes in place, leaving a short tail alone.
Count the nodes on the longest root-to-leaf path.
Mirror a tree by swapping every node's children.
Check whether two trees have identical shape and values.
Group the tree's values row by row, left to right.
Return the rightmost node of every level.
Check that every node fits the range set by all its ancestors.
Walk down a BST until p and q split to different sides.
Find the largest sum along any path, which may bend at one node.
Smash the two heaviest stones together until at most one is left.
Report the k-th largest score after every new score arrives.
Find the k-th largest number without fully sorting.
Return the k points nearest to (0, 0).
Schedule tasks with a cooldown between repeats using as few time slots as possible.
Merge k sorted lists of numbers into one sorted list.
Support adding numbers and reading the running median quickly.
List every time a binary watch can show with a given number of lights on.
Return every subset (the power set) of a list of distinct numbers.
Return every ordering of a list of distinct numbers.
Spell every string a sequence of phone keypad digits could stand for.
Find every multiset of candidates (reuse allowed) that adds up to a target.
Decide whether a word can be traced through neighbouring cells of a letter grid.
Place n queens on an n × n board so that no two attack each other; list every way.
Recolour the region connected to a starting pixel, like a paint-bucket tool.
Decide whether two nodes of an undirected graph are connected.
Count groups of connected land cells in a grid.
Minutes until rot spreads to every orange: multi-source BFS on a grid.
Order courses so every prerequisite comes first, or report a cycle.
Find the edge that turned a tree into a graph with a cycle.
How long a signal from one node takes to reach every node: Dijkstra.
Fewest one-letter changes to turn one word into another: BFS over words.
Count the ways to climb n steps taking 1 or 2 at a time.
Reach the top of the stairs as cheaply as possible, 1 or 2 steps at a time.
Take the most money from a row of houses without taking two neighbours.
Fewest coins that add up to an amount, with unlimited coins of each value.
Length of the longest strictly increasing subsequence.
Can a string be split into a sequence of dictionary words?
Length of the longest subsequence two strings share.
Can the numbers be split into two groups with equal sums?
Fewest insertions, deletions and replacements to turn one word into another.
Hand out cookies to satisfy as many children as possible.
Find the largest sum of any contiguous subarray (Kadane).
Decide whether you can jump from the first index to the last.
Find the fewest jumps needed to reach the last index.
Merge all overlapping intervals into disjoint ones.
Remove the fewest intervals so the rest don't overlap.
Find the minimum number of rooms to host every meeting.
Give out the fewest candies so higher-rated children beat their neighbours.
Every value appears twice except one; find it in O(1) space.
Count the set bits in a non-negative integer.
Set-bit counts for every number from 0 to n, in linear time.
Find the one number from 0..n missing from the list.
Build a trie class with insert, search and starts_with.
A word dictionary whose search supports '.' as a wildcard letter.
Largest a ^ b over all pairs, faster than checking every pair.
Find every word from a list that can be traced on a letter grid.