Understand what the interviewer is asking for by using test cases and questions about the problem.
- Established a set (2-3) of test cases to verify their own solution later.
- Established a set (1-2) of edge cases to verify their solution handles complexities.
- Have fully understood the problem and have no clarifying questions.
- Have you verified any Time/Space Constraints for this problem?
HAPPY CASE
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.
Input: n = 5, bad = 5
Output: 5
Explanation:
call isBadVersion(3) -> false
call isBadVersion(5) -> true
call isBadVersion(4) -> false
call isBadVersion(5) -> true
Then 5 is the first bad version.
EDGE CASE
Input: n = 1, bad = 1
Output: 1
Match what this problem looks like to known categories of problems, e.g. Linked List or Dynamic Programming, and strategies or patterns in those categories.
For Array problems, we want to consider the following approaches:
Plan the solution with appropriate visualizations and pseudocode.
General Idea: We can have a left and right pointer to create a mid point where we can decide whether or not the number exist in the left half or right half, with each iteration until we find our number or we exhaust our list. Binary Search
1. Initialize left and right pointers
2. While left pointer is less than right pointer we have not exhausted the versions
a. Get the mid point of the two pointers
b. Check if mid point is bad or not
i. If mid point is bad then there might be more bad versions before it, shift right pointer and check for earlier bad version.
ii. If mid point is good then we know that everything to the left is good, shift left pointer to check for bad version in right half
3. Return the right pointer for first bad version.
⚠️ Common Mistakes
Implement the code to solve the algorithm.
# The isBadVersion API is already defined for you.
# def isBadVersion(version: int) -> bool:
class Solution:
def firstBadVersion(self, n: int) -> int:
# Initialize left and right pointers
l, r = 1, n
# While left pointer is less than right pointer we have not exhausted the versions
while l < r:
# Get the mid point of the two pointers
mid = (l + r) // 2
# Check if mid point is bad or not
if isBadVersion(mid):
# If mid point is bad then there might be more bad versions before it, shift right pointer and check for earlier bad version.
r = mid
else:
# If mid point is good then we know that everything to the left is good, shift left pointer to check for bad version in right half
l = mid + 1
# Return the right pointer for first bad version
return r
public class Solution extends VersionControl {
public int firstBadVersion(int n) {
// Initialize left and right pointers
int l=1;
int r=n;
// While left pointer is less than right pointer we have not exhausted the versions
while(l<r){
// Get the mid point of the two pointers
int m=l+(r-l)/2;
// Check if mid point is bad or not
if(isBadVersion(m)){
// If mid point is bad then there might be more bad versions before it, shift right pointer and check for earlier bad version.
r=m;
}else{
// If mid point is good then we know that everything to the left is good, shift left pointer to check for bad version in right half
l=m+1;
}
}
// Return the right pointer for first bad version
return r;
}
}
Review the code by running specific example(s) and recording values (watchlist) of your code's variables along the way.
Evaluate the performance of your algorithm and state any strong/weak or future potential work.
Assume N
represents the number of items in the array.
O(logN)
because we can eliminate half the possible versions with each check.O(1)
because we only need two pointers to do the job.