First Bad Version Coding Interview Question | Skilled.dev
Interview Question

First Bad Version

You have been working on a key feature for your company's application, but after coding for months, you realize you need a break and decide to go on vacation. When you return, you pull all the latest code from the main branch into your feature branch... But there's an issue — the code is now broken.

You know that your code was working before you left, but one of the commits introduced from the main branch causes a bug with your feature (womp womp).

You know that all the commits before the one that caused the bug must still work, but then any of the commits after the breaking change was added must all be broken. It must look something like the following:

[good, good, good, good, bad, bad, bad]

You also have a helper function isBadVersion where you invoke it with a version number, and the function will return a boolean false if the version is working or true if the version is broken.

The input to the solution function is an integer that represents the total number of versions, and the output will be the first version that's broken. Write a function firstBadVersion that returns the version number of the first commit that is broken.

So if the input is 100, there are 100 total versions. We need to determine the version between 1 and 100 that represents the first broken commit.

 

Breakdown

Loading...

Table of Contents

Validate My Answer

  1. The brute force way to approach this is to start at the beginning and iterate through each item until you find the first breaking change. This solution is O(n) time, but you can do better.

  2. The optimal solution operates in O(log n) time and O(1) space. To solve this question, you'll utilize a new approach on a core searching concept.

Loading...

Table of Contents

Test Results

Run your code and see results here...

/**
 * NOTE: function isBadVersion is defined in the test scope
 * and will be called correctly when used in this solution function.
 */

/**
 * @param {number} totalVersions
 * @return {number}
 */
const firstBadVersion = (totalVersions) => {
  // Your solution here
}
// Upgrade for full course access
// Upgrade for full course access

First Bad Version

You have been working on a key feature for your company's application, but after coding for months, you realize you need a break and decide to go on vacation. When you return, you pull all the latest code from the main branch into your feature branch... But there's an issue — the code is now broken.

You know that your code was working before you left, but one of the commits introduced from the main branch causes a bug with your feature (womp womp).

You know that all the commits before the one that caused the bug must still work, but then any of the commits after the breaking change was added must all be broken. It must look something like the following:

[good, good, good, good, bad, bad, bad]

You also have a helper function isBadVersion where you invoke it with a version number, and the function will return a boolean false if the version is working or true if the version is broken.

The input to the solution function is an integer that represents the total number of versions, and the output will be the first version that's broken. Write a function firstBadVersion that returns the version number of the first commit that is broken.

So if the input is 100, there are 100 total versions. We need to determine the version between 1 and 100 that represents the first broken commit.

 
/**
 * NOTE: function isBadVersion is defined in the test scope
 * and will be called correctly when used in this solution function.
 */

/**
 * @param {number} totalVersions
 * @return {number}
 */
const firstBadVersion = (totalVersions) => {
  // Your solution here
}
// Upgrade for full course access
// Upgrade for full course access