Practice SetLesson 101 min read

Binary Search — Practice Problems

November 05, 2026

// How to use this set

Work these in order: easy (Binary Search, Search Insert Position, First Bad Version) → medium (Search in Rotated Sorted Array, Find Minimum in Rotated Array, Find First/Last Position, Search a 2D Matrix, Find Peak Element, Koko Eating Bananas, Ship Within D Days, Time-Based Key-Value Store) → hard (Split Array Largest Sum, Median of Two Sorted Arrays). Watch the loop invariant (lo<=hi vs lo<hi) and the 'search on the answer' trick where the array you binary-search over is the space of possible answers. Every solution below was run and tested.

Thirteen problems that drill the binary-search pattern from Lesson 10 — the plain “find an element” variety, the rotated-array twists, and the powerful “binary search on the answer” family. Open a problem, try it yourself first, then read how to reach the solution. Each solution here was actually run and tested.

How to practise. The bugs in binary search are always the boundaries. Before you code, fix two things in your head: the invariant (is the answer always inside [lo, hi]?) and the exit condition (lo <= hi when you return from inside the loop; lo < hi when the answer is wherever the pointers meet). For the “search on the answer” ones, first write the ok(x) feasibility check, then binary-search the smallest x that passes.

Three of the solutions below use the standard library — add these imports at the top:

import bisect
import math
from collections import defaultdict

The 13 problems

  1. 1Binary Search (LeetCode 704)EasyFind a target's index in a sorted array: halve the search range each step by comparing with the middle.arrow_forward
  2. 2Search Insert Position (LeetCode 35)EasyReturn the target's index, or where it would be inserted: find the first element >= target, i.e. bisect_left.arrow_forward
  3. 3First Bad Version (LeetCode 278)EasyFind the first bad version with few API calls: binary-search the boundary where good turns to bad.arrow_forward
  4. 4Search in Rotated Sorted Array (LeetCode 33)MediumSearch a rotated sorted array in O(log n): one half around mid is always sorted, so test the target there.arrow_forward
  5. 5Find Minimum in Rotated Sorted Array (LeetCode 153)MediumFind the minimum of a rotated sorted array: compare mid with the right end to see which side holds the drop.arrow_forward
  6. 6Find First and Last Position of Element in Sorted Array (LeetCode 34)MediumFind the first and last index of a target in a sorted array with two boundary searches.arrow_forward
  7. 7Search a 2D Matrix (LeetCode 74)MediumSearch a row-major sorted matrix: treat it as one sorted array of R*C cells and map indices with divmod.arrow_forward
  8. 8Find Peak Element (LeetCode 162)MediumFind any peak in an unsorted array in O(log n): compare mid with its right neighbour and always walk uphill.arrow_forward
  9. 9Koko Eating Bananas (LeetCode 875)MediumFind the slowest eating speed that finishes in h hours: binary-search the speed, checking feasibility.arrow_forward
  10. 10Capacity To Ship Packages Within D Days (LeetCode 1011)MediumFind the least ship capacity that delivers packages in order within D days: binary-search it, pack greedily.arrow_forward
  11. 11Time Based Key-Value Store (LeetCode 981)MediumStore timestamped values per key; get(key, t) binary-searches for the latest timestamp at or before t.arrow_forward
  12. 12Split Array Largest Sum (LeetCode 410)HardSplit an array into k parts minimising the largest sum: binary-search the cap and count pieces greedily.arrow_forward
  13. 13Median of Two Sorted Arrays (LeetCode 4)HardFind the median of two sorted arrays in O(log min(m, n)): binary-search a partition of the smaller array.arrow_forward