Skip to content
Decoder

First Bad Version

Easy

Problem

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.

Example 1

Input
n = 5, first bad = 4
Output
4
Why
3 is good, 4 and 5 are bad

Example 2

Input
n = 1, first bad = 1
Output
1

Constraints

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

Read it once before you touch anything

Want some pressure? Start the timer. In an interview you get about two minutes to understand the problem.

  1. 1Read the goalWhat goes in, what comes out
  2. 2Scan the examplesCheck one by hand
  3. 3Note the constraintsThey set your Big O
Say what it asks in one sentence
Say it out loud first, then compare with ours.