18 · Binary Search

#278 First Bad Version

Easysolve on LeetCode ↗

frequently reported atMetaGoogleAmazonMicrosoft

the problem

In plain words

Your product has versions 1 to n. One version was bad, and every version built after it is bad too. You may call isBadVersion(version) — LeetCode provides it through a parent class VersionControl that your Solution extends. Return the first bad version, calling the API as few times as you can.

in n = 5, bad = 4

out 4

isBadVersion(3) = false, isBadVersion(4) = true, isBadVersion(5) = true — so 4 is the first bad one.

in n = 1, bad = 1

out 1

  • 1 ≤ bad ≤ n ≤ 2³¹ − 1

your turn

Try it before you peek

Read the examples, say the brute force out loud, then write code: here or on LeetCode ↗. Stuck for 20 minutes? Open one hint at a time.

Solve it in the browser

LeetCode-style: complete the class. Compiled by the real javac (Java 17) and judged right here — nothing is sent anywhere.

    Solutions are hidden so you can try first. Even 20 minutes of real effort makes the answer stick far better than reading it.

    Marking the problem solved, or opening every hint, also shows it.

    practise next

    Similar problems

    1. #35
      Search Insert Position
      Easyin Binary Search
    2. #34
    3. #875
      Koko Eating Bananas
      Mediumin Binary Search