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
2. Pattern 1 · Classic binary search
3. Pattern 2 · Boundaries
4. Pattern 3 · Rotated sorted arrays
5. Pattern 4 · Searching a 2D matrix
6. Pattern 5 · Binary search on the answer
7. Pattern 6 · Hard binary search
Related courses
- SortingComparison and non-comparison based sorting — from bubble sort intuition to quicksort partitioning.8 lessons · 16 problems
- ArraysContiguous, indexable memory — the foundation almost every other data structure builds on.12 lessons · 16 problems
- Binary TreeHierarchical structures for representing nested relationships and enabling fast search.5 lessons · 16 problems
