278. First Bad Version
First Bad Version (LeetCode 278) is a Easy Data Structures and Algorithms interview problem you can solve, run and submit on Thita.ai. It belongs to the Binary Search Patterns pattern, in the On Sorted Array/List subpattern.
How to solve First Bad Version
First Bad Version is a Binary Search on Sorted Array problem, part of Binary Search Patterns. Master standard binary search on sorted arrays. Learn search insert position, sqrt calculation, and 2D matrix search.
Work towards the solution in these steps:
- Since the versions are sequential and there is a point where versions transition from good to bad, consider if there is a searching technique that works well on sorted or monotonic sequences.
- Think about how you can use the given isBadVersion API to eliminate half of the versions in each step instead of checking them one by one.
- Remember that binary search works by maintaining two pointers or indices to narrow down the search space; consider how to define these pointers for the version numbers.
The reference solution runs in O(log n) time and O(1) space.
The First Bad Version solution and editorial walks through the full approach with reference code.
Problem statement
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 <= 2^31 - 1
Topics and companies
Topics: Binary Search, Interactive.
Reported in interviews at Facebook.
How to practise First Bad Version on Thita.ai
Starter code is provided in C, C#, C++, Go, Java, JavaScript and Python. Your solution runs against 5 test cases for this problem, with the AI coach available for a hint when you are stuck rather than a finished answer. The reference solution runs in O(log n) time and O(1) space.
Open First Bad Version in the code editor, or read the First Bad Version editorial for a worked solution with its approach and complexity analysis.
Where First Bad Version sits in the DSA pattern sheet
Problems related to First Bad Version
Other problems that use the same On Sorted Array/List and Binary Search Patterns techniques:
- Binary Search — Easy, same subpattern
- Guess Number Higher or Lower — Easy, same subpattern
- Kth Missing Positive Number — Easy, same subpattern
- Search a 2D Matrix — Medium, same subpattern
- Search Insert Position — Easy, same subpattern
- Single Element in a Sorted Array — Medium, same subpattern
First Bad Version FAQ
What is the LeetCode number of First Bad Version?
First Bad Version is LeetCode problem 278, rated Easy.
What pattern is used to solve First Bad Version?
First Bad Version is a Binary Search on Sorted Array problem. It belongs to the Binary Search Patterns pattern, in the On Sorted Array/List subpattern.
What is the time complexity of First Bad Version?
The reference solution runs in O(log n) time and O(1) space.
Which companies ask First Bad Version?
First Bad Version has been reported in interviews at Facebook.