Teaching Kids Programming – Binary Search Algorithm to Find First Bad Version


Teaching Kids Programming: Videos on Data Structures and Algorithms

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.

Example 1:
Input: n = 5, bad = 4
Output: 4
Explanation:
call isBadVersion(3) – false
call isBadVersion(5) – true
call isBadVersion(4) – true
Then 4 is the first bad version.

Example 2:
Input: n = 1, bad = 1
Output: 1

Constraints:
1 <= bad <= n <= pow(2,31) – 1

Bruteforce Linear Scan to Find Bad Version

We can scan from the first until we meet the first bad version. This is linear time complexity:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# The isBadVersion API is already defined for you.
# @param version, an integer
# @return an integer
# def isBadVersion(version):
 
class Solution:
    def firstBadVersion(self, n):
        """
        :type n: int
        :rtype: int
        """
        for i in range(1, n + 1):
            if isBadVersion(i):
                return i
# The isBadVersion API is already defined for you.
# @param version, an integer
# @return an integer
# def isBadVersion(version):

class Solution:
    def firstBadVersion(self, n):
        """
        :type n: int
        :rtype: int
        """
        for i in range(1, n + 1):
            if isBadVersion(i):
                return i

Binary Search Algorithm to Locate the First Bad Version

Since the domain scope is non-descending. We can use binary search algorithm to reduce the time complexity from O(N) linear to O(logN). The left and right bound are initialised to 1 to n respectively. Then, we check the middle, if the middle version is still good, the answer should be located in the upper half, otherwise, we can continue search the lower half (to look for the first bad version).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# The isBadVersion API is already defined for you.
# @param version, an integer
# @return an integer
# def isBadVersion(version):
 
class Solution:
    def firstBadVersion(self, n):
        """
        :type n: int
        :rtype: int
        """
        left, right = 1, n
        while left <= right:
            mid = (left + right) // 2
            if isBadVersion(mid):
                right = mid - 1
            else:
                left = mid + 1
        return left
# The isBadVersion API is already defined for you.
# @param version, an integer
# @return an integer
# def isBadVersion(version):

class Solution:
    def firstBadVersion(self, n):
        """
        :type n: int
        :rtype: int
        """
        left, right = 1, n
        while left <= right:
            mid = (left + right) // 2
            if isBadVersion(mid):
                right = mid - 1
            else:
                left = mid + 1
        return left

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
487 words
Last Post: Dynamic Programming Algorithms to Compute the Longest Chain of Blocks
Next Post: Binary Search Algorithm to Find Next Node on Its Right in a Binary Tree

The Permanent URL is: Teaching Kids Programming – Binary Search Algorithm to Find First Bad Version

Leave a Reply