Skip to content

visual walkthrough

First Bad Version

EasyLower / Upper BoundReported at: GoogleAmazonMeta+4

Binary search over a boolean predicate, the foundation of search-on-answer.

Solve on LeetCode

The idea

The versions look like good, good, good, bad, bad, bad. You want the first bad one with as few isBad calls as possible.

Ask about the middle: a bad answer means the boundary is at or before it; a good answer means it's after.

Complexity

approachtimespace
Check every versionO(n)O(1)
Binary searchO(log n)O(1)

More walkthroughs