Skip to content

First Bad Version

EasyBinary Search
Decode it first. Clues, constraints, pattern and plan before any code.

Your product has versions 1 to n. One release introduced a bug, and every version after a bad one is also bad. You are given a function is_bad(version) (TypeScript: isBad(version)) that returns true for bad versions. Return the first bad version.

Each call to is_bad is expensive (imagine a full test run), and the tests count them, so call it as few times as you can.

n = 5, first bad = 4   →  4   (3 is good, 4 and 5 are bad)
n = 1, first bad = 1   →  1

Constraints

  • 1 <= bad <= n <= 2^31 - 1
  • At least one version is bad.