EvalFrame Technical Mock Interviews & Coding Practice

First Bad Version

Easy · Binary Search · Algorithm problem · all problems

You are a product manager and currently leading a team to develop a new product. Unfortunately, the latest version of your product fails the quality check. Since each version is developed based on the previous version, all the versions after a bad version are also bad. Suppose you have `n` versions `[1, 2, ..., n]` and you want to find out the first bad one, which causes all the following ones to be bad. You are given an API `bool isBadVersion(version)` which returns whether `version` is bad. Implement a function to find the first bad version. You should minimize the number of calls to the API. **Examples:** ```typescript // Given n = 5, and version = 4 is the first bad version. solution(isBadVersion)(5) // 4 ```

Solve It in the BrowserPractice Explaining It Out Loud

Optimal Approach

  • Time complexity: O(log n)
  • Space complexity: O(1)

Standard binary search divides the search space in half with every query, making at most log2(n) API calls.

Follow-Up Questions Interviewers Ask About First Bad Version

  • Why do we compute mid as left + (right - left) / 2?
    To prevent potential 32-bit integer overflow that can occur when doing (left + right) / 2 in languages with fixed-size integers like C++ and Java.

Practice the full problem with execution, tests, and AI code review in the interactive lab — then rehearse explaining your solution in a live voice mock interview.