Binary search coding interview questions 12 questions in 4 chapters

  1. 01Classic binary search
  2. 02Boundary binary search5 questions
  3. 03Binary search on the answer4 questions
  4. 04Partition binary search

Classic binary search

A sorted array and an exact target, or the insertion point for that target.

  • 1.1 Binary Search Easy 3 min

    The array is sorted, so I can throw away half of it at each step. I pick a mid index with lo + (hi minus lo) / 2, which avoids overflowing the lo + hi sum, and I move lo or hi according to whether mid is less than or greater than the target. If the range becomes empty I return -1.

    Read the full answer
  • 1.2 Search Insert Position Easy 3 min

    I need the index of the target, or the index where it would be inserted to keep the array sorted. That is a lower bound: the first index whose value is greater than or equal to the target. I use a half-open range and move lo up when mid is strictly less than the target.

    Read the full answer

Boundary binary search

The first or last index where a predicate becomes true: a run of duplicates, a rotation, a peak, or a flattened matrix.

  • 2.1 Find First and Last Position of Element in Sorted Array Medium 3 min

    The array is sorted and may contain duplicates, so I need the first and last index of the target, or [-1, -1] if it is absent. I run a lower-bound search for the target and another for target + 1. The first answer is the left edge; the second minus one is the right edge, provided the left edge actually holds the target.

    Read the full answer
  • 2.2 Find Minimum in Rotated Sorted Array Medium 3 min

    A rotated sorted array of unique values is two increasing runs, and the minimum is the start of the second run, or the first element if the array was not rotated. I compare mid with the right end: if mid is greater than the right, the minimum is to the right of mid; otherwise mid itself may be the minimum and I search to the left.

    Read the full answer
  • 2.3 Find Peak Element Medium 3 min

    A peak is an element strictly greater than its neighbours, and the official problem treats the positions just outside the array as negative infinity, so a peak always exists. I look at mid and mid + 1: if mid is less than mid + 1, a peak lies to the right, otherwise mid itself may be a peak and I search to the left. Any peak is accepted.

    Read the full answer
  • 2.4 Search a 2D Matrix Medium 3 min

    Each row is sorted, and the first value of a row is greater than the last value of the previous row, so the matrix is a single sorted sequence in row-major order. I binary-search the range 0 to rows times columns minus 1, and I map mid to row mid / columns and column mid % columns.

    Read the full answer
  • 2.5 Search in Rotated Sorted Array Medium 3 min

    The array was sorted and then rotated, and I must find a target in logarithmic time. At mid, at least one half is still sorted. I check whether the target lies inside the sorted half, and I throw the other half away; if it does not, I search the unsorted half, which is itself a smaller rotated array.

    Read the full answer

Binary search on the answer

Guess a numeric answer and run a monotonic feasibility check, then shrink the range to the smallest (or largest) value that still works.

  • 3.1 Sqrtx Easy 3 min

    I need the integer square root of a non-negative int, meaning the largest integer whose square is at most x. I binary-search the answer in 1 to x / 2, compare mid times mid against x using a long so the product cannot overflow, and keep the last mid that was small enough.

    Read the full answer
  • 3.2 Koko Eating Bananas Medium 3 min

    Koko must finish every pile in h hours, eating at a fixed integer speed, and she wants the slowest speed that still works. I binary-search the speed between 1 and the largest pile. For each mid speed I add up the hours, using ceiling division, and I move the search toward the smallest speed whose hours stay within h.

    Read the full answer
  • 3.3 Capacity To Ship Packages Within D Days Medium 3 min

    Packages must be shipped in order, and I need the smallest ship capacity that lets me finish in at most days days. I binary-search the capacity between the heaviest package and the sum of all weights. For each mid capacity I count how many days a greedy left-to-right packing would take, and I search for the smallest capacity whose day count fits.

    Read the full answer
  • 3.4 Minimum Time Required Medium 3 min

    Each machine produces one item every machines[i] days, all running at once, and I need the fewest days to produce a given goal. I binary-search the number of days. For a candidate day count I add days / machines[i] for each machine, stopping early if the total already meets the goal, and I search for the first day count that is enough.

    Read the full answer

Partition binary search

Cut two sorted arrays so the left half contains the median position.

  • 4.1 Median of Two Sorted Arrays Hard 4 min

    I need the median of two already-sorted arrays in logarithmic time, so I cannot merge them. I binary-search a cut in the shorter array; the cut in the longer array is then fixed by the median position. When every value on the left of the cut is less than or equal to every value on the right, the median is made of the max of the left and, if the total length is even, the min of the right.

    Read the full answer