First Bad Version Problem

First Bad Version Problem — ExecCode Easy DSA Practice

Solve the First Bad Version problem on ExecCode. Free online easy DSA practice in Binary Search. Write and run code in Java, C++, Python — no signup required to run.

Problem description

You are a product manager leading a team to develop a new product. Since each version is built on the previous one, all versions after a bad version are also bad. You have n versions [1, 2, ..., n], and you want to find the first bad one, which causes all the following ones to be bad. You are given an API bool isBadVersion(version) that returns whether a version is bad. Implement a function to find the first bad version while minimizing the number of calls to the API. (For teaching purposes this problem accepts n and the true bad index and wraps isBadVersion internally.)

Examples

Input n = 8, bad = 5; Output 5. Input n = 5, bad = 4; Output 4. Input n = 1, bad = 1; Output 1

Constraints

1 ≤ bad ≤ n ≤ 2³¹ - 1

Practice First Bad Version free on ExecCode. Browse DSA problems, topic map, and placement guides.