Chapter 06 - Binary Search

Halve the search space at every step. It works on sorted arrays, and on any question whose answer is monotonic.

Coding Interview Patterns book cover with the Rust logo and a CHAP 06 label, the chapter on binary search

This page is the entry point of chapter 6 of my Rust solutions to the book Coding Interview Patterns. It sums up the pattern in a few lines, then lists the problems of the chapter. As explained on the series page, the detailed explanations of the algorithms are in the book.

The idea

Binary search keeps two bounds and looks at the middle. Depending on the value found there, it throws away half of the search space. After \(\log_2 n\) steps, one candidate is left. The real power shows when we search on the answer rather than in an array: in Cutting Wood, we look for the highest cutting height that still yields enough wood, because “enough wood” is true up to a point and false after it.

The chapter also has an introduction page with my notes on the key ideas and a checklist.

When to reach for it

  • The input is sorted, even partially (rotated array, sorted matrix).
  • We look for the first or last position that satisfies a condition, or an insertion index.
  • The question reads “the largest (or smallest) value such that…”: search on the answer.

In Rust

  • Compute the middle with left + (right - left) / 2, and watch mid - 1 when mid == 0 on usize.
  • The standard library already has slice::binary_search, which returns Err(index) with the insertion index, and partition_point for the first element that breaks a predicate.
  • Decide the loop condition (< or <=) and the update (mid or mid + 1) together: the checklist of the introduction page is there for that.

Problems in this chapter

  • p101 - Introduction: Introduction to the Binary Search pattern for coding interviews, in Rust: the key idea, when to use it, and the problems of the chapter.
  • p105 - Find insertion index: Find insertion index (Binary Search), solved in Rust. Given a sorted array with unique values.
  • p110 - First and Last Occurence of a Number: First and Last Occurrence of a Number (Binary Search) solved in Rust: in a sorted array, return the first and last indexes of a target number.
  • p116 - Cutting Wood: Cutting Wood (Binary Search) solved in Rust: find the highest cutting height that still yields at least k units of wood from a row of trees.
  • p122 - Find the Target in a Rotated Sorted Array: Find the Target in a Rotated Sorted Array (Binary Search) solved in Rust: return the index of a target value in a rotated sorted array, or -1.
  • bonus039 - Find the Median From Two Sorted Arrays: Find the Median From Two Sorted Arrays (Binary Search) solved in Rust: the median of two sorted arrays as if they were merged, without merging them.
  • bonus046 - Matrix Search: Matrix Search (Binary Search), solved in Rust. Determine if a target value exists in a matrix. Each row of the matrix is sorted in non-decreasing order. The…
  • bonus052 - Local Maxima: Local Maxima (Binary Search), solved in Rust. A local maxima is a value greater than both its immediate neighbors. Return any local maxima in an array. You…
  • bonus058 - Weighted Random Selection: Weighted Random Selection (Binary Search), solved in Rust. Given an array of items, each with a corresponding weight. Implement a function that randomly…

Other chapters

All the chapters, with every problem, are listed on the series page.


Back to top

Published on: Dec 1 2026 at 10:00 AM | Last updated: Dec 1 2026 at 10:32 AM

Copyright © 1964-2026 - 40tude