Python coding
195 interview problems by pattern. Write the function and run it against the tests right here: Python runs in your browser.
Hash maps, sets and prefix sums
- Group words by their letterseasy
- Two positions that hit a targeteasy
- Strings with the same shapeeasy
- Check a half-filled sudokumedium
- Count runs that sum to kmedium
- Fraction as a repeating decimalmedium
- Longest streak of consecutive numbersmedium
- Pairs with mismatched gapsmedium
- Zero-sum picks from four listsmedium
- Most points on one linehard
Prefix sums and difference arrays
- Balance point of a listeasy
- Sums of many rangeseasy
- Count runs with a sum divisible by kmedium
- Counters after many range updatesmedium
- Longest run with equal 0s and 1smedium
- Longest stretch with even vowel countsmedium
- Product of all the othersmedium
- Sums of many rectangles in a gridmedium
- Shortest run reaching a total, with negativeshard
- Sub-rectangles that sum to a targethard
Two pointers
- Palindrome ignoring punctuationeasy
- Squares back in ordereasy
- Target-sum pair in a sorted listeasy
- Fewest two-seat boatsmedium
- Group three colors in one passmedium
- Keep at most k copies, in placemedium
- Most water between two wallsmedium
- Triples that make a trianglemedium
- Triplets that cancel outmedium
- Rain trapped between barshard
Sliding window
- Best total over k consecutive dayseasy
- A repeat within k positionseasy
- Every place an anagram startsmedium
- Count runs with a product below kmedium
- Longest run without repeatsmedium
- Longest single-letter run with k editsmedium
- Most points from the ends of a rowmedium
- Shortest run of positives reaching a totalmedium
- Count runs with exactly k distinct valueshard
- Smallest window covering every needed characterhard
Binary search and search on the answer
- Integer square rooteasy
- Peak of a mountain listeasy
- Find a value in a rotated sorted listmedium
- Find a value in a sorted gridmedium
- First and last index of a valuemedium
- Key-value store with historymedium
- Smallest truck that meets the deadlinemedium
- Widest spacing for k sensorsmedium
- K-th smallest distance among pairshard
- Median across two sorted listshard
Linked lists
- Node at the halfway pointeasy
- Linked list, back to fronteasy
- Splice two sorted listseasy
- Add numbers stored as digit listsmedium
- Clone a list with jump linksmedium
- First node of the cyclemedium
- Interleave the two ends of a listmedium
- List that reads the same backwardsmedium
- Unlink the k-th node from the backmedium
- Flip a list in blocks of khard
Binary trees and BSTs
- Depth of the deepest leafeasy
- Mirror a binary treeeasy
- Closest shared ancestormedium
- Downward paths adding to a targetmedium
- Is it a search tree?medium
- Every node k steps awaymedium
- Rebuild a tree from its traversalsmedium
- Rightmost value on each levelmedium
- Heaviest path in a treehard
- Tree to text and backhard
Stacks and monotonic stacks
- Brackets that close in ordereasy
- Convoys at the finish linemedium
- Days until a warmer daymedium
- Next greater value around a circlemedium
- Smallest number after removing k digitsmedium
- Sum of every run's minimummedium
- Largest rectangle under barshard
- Longest well-formed parentheses stretchhard
- Maximum of every windowhard
- How many people each person can seehard
Heaps and top-K
- Last stone left after smashingeasy
- Farthest rooftop with bricks and laddersmedium
- The k points nearest the originmedium
- The k smallest pair sumsmedium
- Merge many sorted listsmedium
- The k most frequent valuesmedium
- Task order on a single coremedium
- Median after each new numberhard
- Most capital after k projectshard
- Rain trapped on a height maphard
Interval problems: sort and sweep
- Attend every meetingeasy
- Add a range to sorted rangesmedium
- Fewest bookings to cancelmedium
- Fewest meeting roomsmedium
- Free time shared by everyonemedium
- Combine overlapping rangesmedium
- Overlap of two schedulesmedium
- Ranges not inside anothermedium
- Most pay from gigs that don't clashhard
- Tightest range around each pointhard
Graph traversal: BFS and DFS
- Repaint a connected patcheasy
- Unlock every roomeasy
- Count the islandsmedium
- Deep copy of a graphmedium
- Fewest steps through a mazemedium
- Minutes until every tree burnsmedium
- Rain that reaches both seasmedium
- Split rivals into two teamsmedium
- Fewest letter changes between two wordshard
- Fewest steps, breaking up to k wallshard
Topological sort
- Starting steps that reach everythingeasy
- Can you finish every course?medium
- Course order, lowest number firstmedium
- Earliest finish for dependent tasksmedium
- Is it a prerequisite, even indirectly?medium
- Rules that allow only one ordermedium
- States that always haltmedium
- Alphabet from a sorted word listhard
- Most repeated label on a pathhard
Union-Find (Disjoint Set Union)
- Cheapest cables to connect every sitemedium
- Count the connected groupsmedium
- Extra cable in the networkmedium
- Merge contact cards that share an emailmedium
- Remove stones sharing a row or columnmedium
- Smallest string from allowed swapsmedium
- Unit conversions from known factsmedium
- Islands after each new land cellhard
- Reachable using only short roadshard
Shortest Paths (BFS, Dijkstra, Bellman-Ford)
- Budget trip with a stop limitmedium
- Count the fastest routesmedium
- Many distance queries on a small mapmedium
- Fewest walls to break throughmedium
- Signal arrival timemedium
- Route with the smallest worst stepmedium
- A toll loop that pays youmedium
- Cheapest drive with a limited tankhard
- Second fastest arrivalhard
Dynamic Programming
- Cheapest climb up the stairseasy
- Cheapest path down a triangleeasy
- Most pay without back-to-back shiftseasy
- Count the palindromes inside a stringmedium
- Count routes around blocked cellsmedium
- Fewest coins for an amountmedium
- Fewest edits between two wordsmedium
- Largest square of onesmedium
- Longest rising subsequencemedium
- Longest shared subsequencemedium
- Split into two equal sumsmedium
- Trading profit with a rest daymedium
- Two strings woven into a thirdmedium
- Ways to pay an amountmedium
- Ways to read a digit codemedium
- Longest chain of nesting boxeshard
- Match text against dots and starshard
- Most points for clearing a rowhard
Greedy algorithms
- Can you reach the last tile?easy
- Largest run sumeasy
- Most children fedeasy
- Balanced parentheses with wildcardsmedium
- Cheapest even split between two citiesmedium
- Fewest jumps to the last tilemedium
- Most pieces with no shared lettersmedium
- Shortest schedule with a cooldownmedium
- Where to start a full lapmedium
- Fewest tokens for a scored rowhard
- Most tasks done before their deadlineshard
Backtracking
- Every subseteasy
- Words a phone keypad can spelleasy
- Balanced brackets up to a depthmedium
- Deal numbers into k equal pilesmedium
- Every distinct orderingmedium
- Every way to cut into palindromesmedium
- Reach a target, each item oncemedium
- Reach a target, reuse allowedmedium
- Every way to split text into wordshard
- Queens that never attackhard
Tries (Prefix Trees)
Design a data structure (LRU cache)
- Queue built from two stackseasy
- Rate limiter over a sliding windoweasy
- Stack that knows its minimumeasy
- Cache that evicts the stalest keymedium
- Feed of the newest postsmedium
- Set with O(1) removal and indexingmedium
- Price tracker with late correctionsmedium
- Tic-tac-toe referee in O(1) per movemedium
- Cache that evicts the least-used keyhard
- Stack that pops the most frequent valuehard