Medium LeetCode Problems: All 1,579, With Python Solutions

Medium is where most interview questions actually sit. The technique is no longer stated — recognising which pattern applies is the problem, and the implementation is the easy half once you have.

The rating is LeetCode's own, carried through unchanged — we do not re-score anything. This library holds 1,579 Medium-rated problems, 1,191 of them with a complete Python solution, a worked example and complexity analysis.

The patterns behind medium problems

The six patterns with the most Medium-rated problems in this library. Working through a pattern beats working down a difficulty list, because the technique transfers and a difficulty band does not.

  • Hash Map · 372 Medium problems — Trade memory for time: remember what you have seen so the second pass never happens.
  • Math and Number Theory · 244 Medium problems — Find the closed form, the invariant, or the modular identity — and skip the loop entirely.
  • Dynamic Programming · 243 Medium problems — Define a state, write the transition, and stop recomputing the same subproblem.
  • Greedy · 234 Medium problems — Take the locally best option every time — when you can prove that never costs you later.
  • Sorting · 227 Medium problems — Spend O(n log n) once to buy an ordering that makes the rest of the problem trivial.
  • Depth-First Search · 214 Medium problems — Follow one path to its end before trying the next — the default way to explore a graph.

Medium problems

Showing the first 200 of 1,579 problems. Problems with a complete Python solution are listed first, then by ascending problem number.

#ProblemDifficultyTopics
2Add Two NumbersMediumRecursion, Linked List, Math
3Longest Substring Without Repeating CharactersMediumHash Table, String, Sliding Window
5Longest Palindromic SubstringMediumTwo Pointers, String, Dynamic Programming
6Zigzag ConversionMediumString
7Reverse IntegerMediumMath
8String to Integer (atoi)MediumString
11Container With Most WaterMediumGreedy, Array, Two Pointers
12Integer to RomanMediumHash Table, Math, String
153SumMediumArray, Two Pointers, Sorting
163Sum ClosestMediumArray, Two Pointers, Sorting
17Letter Combinations of a Phone NumberMediumHash Table, String, Backtracking
184SumMediumArray, Two Pointers, Sorting
19Remove Nth Node From End of ListMediumLinked List, Two Pointers
22Generate ParenthesesMediumString, Dynamic Programming, Backtracking
24Swap Nodes in PairsMediumRecursion, Linked List
29Divide Two IntegersMediumBit Manipulation, Math
31Next PermutationMediumArray, Two Pointers
33Search in Rotated Sorted ArrayMediumArray, Binary Search
34Find First and Last Position of Element in Sorted ArrayMediumArray, Binary Search
36Valid SudokuMediumArray, Hash Table, Matrix
38Count and SayMediumString
39Combination SumMediumArray, Backtracking
40Combination Sum IIMediumArray, Backtracking
43Multiply StringsMediumMath, String, Simulation
45Jump Game IIMediumGreedy, Array, Dynamic Programming
46PermutationsMediumArray, Backtracking
47Permutations IIMediumArray, Backtracking, Sorting
48Rotate ImageMediumArray, Math, Matrix
49Group AnagramsMediumArray, Hash Table, String +1
50Pow(x, n)MediumRecursion, Math
53Maximum SubarrayMediumArray, Divide and Conquer, Dynamic Programming
54Spiral MatrixMediumArray, Matrix, Simulation
55Jump GameMediumGreedy, Array, Dynamic Programming
56Merge IntervalsMediumArray, Sorting
57Insert IntervalMediumArray
59Spiral Matrix IIMediumArray, Matrix, Simulation
61Rotate ListMediumLinked List, Two Pointers
62Unique PathsMediumMath, Dynamic Programming, Combinatorics
63Unique Paths IIMediumArray, Dynamic Programming, Matrix
64Minimum Path SumMediumArray, Dynamic Programming, Matrix
71Simplify PathMediumStack, String
72Edit DistanceMediumString, Dynamic Programming
73Set Matrix ZeroesMediumArray, Hash Table, Matrix
74Search a 2D MatrixMediumArray, Binary Search, Matrix
75Sort ColorsMediumArray, Two Pointers, Sorting
77CombinationsMediumBacktracking
78SubsetsMediumBit Manipulation, Array, Backtracking
79Word SearchMediumDepth-First Search, Array, String +2
80Remove Duplicates from Sorted Array IIMediumArray, Two Pointers
81Search in Rotated Sorted Array IIMediumArray, Binary Search
82Remove Duplicates from Sorted List IIMediumLinked List, Two Pointers
86Partition ListMediumLinked List, Two Pointers
89Gray CodeMediumBit Manipulation, Math, Backtracking
90Subsets IIMediumBit Manipulation, Array, Backtracking
91Decode WaysMediumString, Dynamic Programming
92Reverse Linked List IIMediumLinked List
93Restore IP AddressesMediumString, Backtracking
95Unique Binary Search Trees IIMediumTree, Binary Search Tree, Dynamic Programming +2
96Unique Binary Search TreesMediumTree, Binary Search Tree, Math +2
97Interleaving StringMediumString, Dynamic Programming
98Validate Binary Search TreeMediumTree, Depth-First Search, Binary Search Tree +1
99Recover Binary Search TreeMediumTree, Depth-First Search, Binary Search Tree +1
102Binary Tree Level Order TraversalMediumTree, Breadth-First Search, Binary Tree
103Binary Tree Zigzag Level Order TraversalMediumTree, Breadth-First Search, Binary Tree
105Construct Binary Tree from Preorder and Inorder TraversalMediumTree, Array, Hash Table +2
106Construct Binary Tree from Inorder and Postorder TraversalMediumTree, Array, Hash Table +2
107Binary Tree Level Order Traversal IIMediumTree, Breadth-First Search, Binary Tree
109Convert Sorted List to Binary Search TreeMediumTree, Binary Search Tree, Linked List +2
113Path Sum IIMediumTree, Depth-First Search, Backtracking +1
114Flatten Binary Tree to Linked ListMediumStack, Tree, Depth-First Search +2
116Populating Next Right Pointers in Each NodeMediumTree, Depth-First Search, Breadth-First Search +2
117Populating Next Right Pointers in Each Node IIMediumTree, Depth-First Search, Breadth-First Search +2
120TriangleMediumArray, Dynamic Programming
122Best Time to Buy and Sell Stock IIMediumGreedy, Array, Dynamic Programming
128Longest Consecutive SequenceMediumUnion Find, Array, Hash Table
129Sum Root to Leaf NumbersMediumTree, Depth-First Search, Binary Tree
130Surrounded RegionsMediumDepth-First Search, Breadth-First Search, Union Find +2
131Palindrome PartitioningMediumString, Dynamic Programming, Backtracking
133Clone GraphMediumDepth-First Search, Breadth-First Search, Graph +1
134Gas StationMediumGreedy, Array
137Single Number IIMediumBit Manipulation, Array
138Copy List with Random PointerMediumHash Table, Linked List
139Word BreakMediumTrie, Memoization, Array +3
142Linked List Cycle IIMediumHash Table, Linked List, Two Pointers
143Reorder ListMediumStack, Recursion, Linked List +1
146LRU CacheMediumDesign, Hash Table, Linked List +1
147Insertion Sort ListMediumLinked List, Sorting
148Sort ListMediumLinked List, Two Pointers, Divide and Conquer +2
150Evaluate Reverse Polish NotationMediumStack, Array, Math
151Reverse Words in a StringMediumTwo Pointers, String
152Maximum Product SubarrayMediumArray, Dynamic Programming
153Find Minimum in Rotated Sorted ArrayMediumArray, Binary Search
155Min StackMediumStack, Design
162Find Peak ElementMediumArray, Binary Search
164Maximum GapMediumArray, Bucket Sort, Radix Sort +1
165Compare Version NumbersMediumTwo Pointers, String
166Fraction to Recurring DecimalMediumHash Table, Math, String
167Two Sum II - Input Array Is SortedMediumArray, Two Pointers, Binary Search
172Factorial Trailing ZeroesMediumMath
173Binary Search Tree IteratorMediumStack, Tree, Design +3
176Second Highest SalaryMediumDatabase
177Nth Highest SalaryMediumDatabase
178Rank ScoresMediumDatabase
179Largest NumberMediumGreedy, Array, String +1
180Consecutive NumbersMediumDatabase
184Department Highest SalaryMediumDatabase
187Repeated DNA SequencesMediumBit Manipulation, Hash Table, String +3
189Rotate ArrayMediumArray, Math, Two Pointers
192Word FrequencyMediumShell
194Transpose FileMediumShell
198House RobberMediumArray, Dynamic Programming
199Binary Tree Right Side ViewMediumTree, Depth-First Search, Breadth-First Search +1
200Number of IslandsMediumDepth-First Search, Breadth-First Search, Union Find +2
201Bitwise AND of Numbers RangeMediumBit Manipulation
204Count PrimesMediumArray, Math, Enumeration +1
207Course ScheduleMediumDepth-First Search, Breadth-First Search, Graph +1
208Implement Trie (Prefix Tree)MediumDesign, Trie, Hash Table +1
209Minimum Size Subarray SumMediumArray, Binary Search, Prefix Sum +1
210Course Schedule IIMediumDepth-First Search, Breadth-First Search, Graph +1
211Design Add and Search Words Data StructureMediumDepth-First Search, Design, Trie +1
213House Robber IIMediumArray, Dynamic Programming
215Kth Largest Element in an ArrayMediumArray, Divide and Conquer, Quickselect +2
216Combination Sum IIIMediumArray, Backtracking
221Maximal SquareMediumArray, Dynamic Programming, Matrix
223Rectangle AreaMediumGeometry, Math
227Basic Calculator IIMediumStack, Math, String
229Majority Element IIMediumArray, Hash Table, Counting +1
230Kth Smallest Element in a BSTMediumTree, Depth-First Search, Binary Search Tree +1
235Lowest Common Ancestor of a Binary Search TreeMediumTree, Depth-First Search, Binary Search Tree +1
236Lowest Common Ancestor of a Binary TreeMediumTree, Depth-First Search, Binary Tree
237Delete Node in a Linked ListMediumLinked List
238Product of Array Except SelfMediumArray, Prefix Sum
240Search a 2D Matrix IIMediumArray, Binary Search, Divide and Conquer +1
241Different Ways to Add ParenthesesMediumRecursion, Memoization, Math +2
260Single Number IIIMediumBit Manipulation, Array
264Ugly Number IIMediumHash Table, Math, Dynamic Programming +1
274H-IndexMediumArray, Counting Sort, Sorting
275H-Index IIMediumArray, Binary Search
279Perfect SquaresMediumBreadth-First Search, Math, Dynamic Programming
284Peeking IteratorMediumDesign, Array, Iterator
287Find the Duplicate NumberMediumBit Manipulation, Array, Two Pointers +1
289Game of LifeMediumArray, Matrix, Simulation
299Bulls and CowsMediumHash Table, String, Counting
300Longest Increasing SubsequenceMediumArray, Binary Search, Dynamic Programming
304Range Sum Query 2D - ImmutableMediumDesign, Array, Matrix +1
306Additive NumberMediumString, Backtracking
307Range Sum Query - MutableMediumDesign, Binary Indexed Tree, Segment Tree +2
309Best Time to Buy and Sell Stock with CooldownMediumArray, Dynamic Programming
310Minimum Height TreesMediumDepth-First Search, Breadth-First Search, Graph +1
313Super Ugly NumberMediumArray, Math, Dynamic Programming
316Remove Duplicate LettersMediumStack, Greedy, String +1
318Maximum Product of Word LengthsMediumBit Manipulation, Array, String
319Bulb SwitcherMediumBrainteaser, Math
322Coin ChangeMediumBreadth-First Search, Array, Dynamic Programming
324Wiggle Sort IIMediumGreedy, Array, Divide and Conquer +2
328Odd Even Linked ListMediumLinked List
331Verify Preorder Serialization of a Binary TreeMediumStack, Tree, String +1
334Increasing Triplet SubsequenceMediumGreedy, Array
337House Robber IIIMediumTree, Depth-First Search, Dynamic Programming +1
341Flatten Nested List IteratorMediumStack, Tree, Depth-First Search +3
343Integer BreakMediumMath, Dynamic Programming
347Top K Frequent ElementsMediumArray, Hash Table, Divide and Conquer +5
355Design TwitterMediumDesign, Hash Table, Linked List +1
357Count Numbers with Unique DigitsMediumMath, Dynamic Programming, Backtracking
365Water and Jug ProblemMediumDepth-First Search, Breadth-First Search, Math
368Largest Divisible SubsetMediumArray, Math, Dynamic Programming +1
371Sum of Two IntegersMediumBit Manipulation, Math
372Super PowMediumMath, Divide and Conquer
373Find K Pairs with Smallest SumsMediumArray, Heap (Priority Queue)
375Guess Number Higher or Lower IIMediumMath, Dynamic Programming, Game Theory
376Wiggle SubsequenceMediumGreedy, Array, Dynamic Programming
377Combination Sum IVMediumArray, Dynamic Programming
378Kth Smallest Element in a Sorted MatrixMediumArray, Binary Search, Matrix +2
380Insert Delete GetRandom O(1)MediumDesign, Array, Hash Table +2
382Linked List Random NodeMediumReservoir Sampling, Linked List, Math +1
384Shuffle an ArrayMediumDesign, Array, Math +1
385Mini ParserMediumStack, Depth-First Search, String
386Lexicographical NumbersMediumDepth-First Search, Trie
388Longest Absolute File PathMediumStack, Depth-First Search, String
390Elimination GameMediumRecursion, Math
393UTF-8 ValidationMediumBit Manipulation, Array
394Decode StringMediumStack, Recursion, String
395Longest Substring with At Least K Repeating CharactersMediumHash Table, String, Divide and Conquer +1
396Rotate FunctionMediumArray, Math, Dynamic Programming
397Integer ReplacementMediumGreedy, Bit Manipulation, Memoization +1
398Random Pick IndexMediumReservoir Sampling, Hash Table, Math +1
399Evaluate DivisionMediumDepth-First Search, Breadth-First Search, Union Find +4
400Nth DigitMediumMath, Binary Search
402Remove K DigitsMediumStack, Greedy, String +1
406Queue Reconstruction by HeightMediumBinary Indexed Tree, Segment Tree, Array +1
413Arithmetic SlicesMediumArray, Dynamic Programming, Sliding Window
416Partition Equal Subset SumMediumArray, Dynamic Programming
417Pacific Atlantic Water FlowMediumDepth-First Search, Breadth-First Search, Array +1
419Battleships in a BoardMediumDepth-First Search, Array, Matrix
421Maximum XOR of Two Numbers in an ArrayMediumBit Manipulation, Trie, Array +1
423Reconstruct Original Digits from EnglishMediumHash Table, Math, String
424Longest Repeating Character ReplacementMediumHash Table, String, Sliding Window
427Construct Quad TreeMediumTree, Array, Divide and Conquer +1
429N-ary Tree Level Order TraversalMediumTree, Breadth-First Search
430Flatten a Multilevel Doubly Linked ListMediumDepth-First Search, Linked List, Doubly-Linked List

Everything at once: the full LeetCode problem list.

A medium problem, live, with someone watching

Stealth Interview is a desktop app for macOS and Windows. It reads the coding problem off your screen, returns a working solution with a step-by-step explanation and its time and space complexity, and transcribes what the interviewer is saying — while staying invisible to screen sharing.