Esc
  • Loading…
↑ ↓ to moveEnter to openEsc to close

Searching

Learn how linear and binary search work, master the binary search templates, then solve the interview questions where halving the search space is the key.

  • 5 Lessons
  • 22 Problems
  • 52 Illustrations

What you will learn

  • How linear search and binary search work, and when each one applies
  • A bug-free binary search: the loop invariant, the safe midpoint and the off-by-one traps
  • The lower-bound and upper-bound templates for first and last positions
  • Binary search on the answer: searching a range of possible answers instead of an array
  • 6 searching patterns: classic search, boundaries, rotated arrays, 2D matrices, search on the answer and the hard cases
  • The searching interview questions asked most, from Binary Search to Median of Two Sorted Arrays

Why this course

Searching is how a program finds things. Checking every element works anywhere but costs O(n); binary search halves the remaining candidates at every step and finds anything among a billion sorted values in about 30 comparisons.

The introduction shows how each search works — linear search, binary search, the lower and upper bound templates, and binary search on the answer — with a drawing and a simulator for every one.

Then come 6 patterns, from easiest to hardest, each taught through the interview questions that use it, with code in four languages, the complexity of every approach and a simulator you can step through.

Requirements

  • Comfort writing loops, conditionals and functions in at least one language
  • The Arrays course, or equivalent familiarity with indexes
  • The Sorting course helps: binary search needs data in order

Course Contents