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 = 10, pick = 6
Output: 6
Input: n = 1, pick = 1
Output: 1
EDGE CASE
Input: n = 2, pick = 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:
⚠️ Common Mistakes
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 distinguish between the smaller and larger half, where we can find our number.
1. Initialize left and right pointers
2. While left pointer is less than or equal to right pointer we have not exhausted the possible numbers
a. Get the mid point of the two pointers
b. Check if mid point is less than, greater than, or equal to the picked number.
i. If mid point number is greater than the picked number, then we need to check the smaller half for the picked number. Set the right pointer to mid pointer - 1
ii. If mid point number is less than the picked number, then we need to check the larger half for the picked number. Set the left pointer to mid pointer + 1
iii. If mid point number is equal to picked number, then return the picked number
Implement the code to solve the algorithm.
# The guess API is already defined for you.
# @param num, your guess
# @return -1 if num is higher than the picked number
# 1 if num is lower than the picked number
# otherwise return 0
# def guess(num: int) -> int:
class Solution:
def guessNumber(self, n: int) -> int:
# Initialize left and right pointers
l, r = 1, n
# While left pointer is less than or equal to right pointer we have not exhausted the possible numbers
while l <= r:
# Get the mid point of the two pointers
mid = (l+r) // 2
# Check if mid point is less than, greater than, or equal to the picked number.
guessResult = guess(mid)
match guessResult:
# If mid point number is greater than the picked, then we need to check the smaller half for the picked number. Set the right pointer to mid pointer - 1
case -1:
r = mid - 1
# If mid point number is less than the picked, then we need to check the larger half for the picked number. Set the left pointer to mid pointer + 1
case 1:
l = mid + 1
# If mid point number is equal to picked number, then return the picked number
case 0:
return mid
case _:
print("Error")
public class Solution extends GuessGame {
public int guessNumber(int n) {
// Initialize left and right pointers
int start=1, end=n, mid;
// While left pointer is less than or equal to right pointer we have not exhausted the possible numbers
while( start<=end){
// Get the mid point of the two pointers
mid= start+(end- start)/2;
// If mid point number is equal to picked number, then return the picked number
if( guess(mid)==0) return mid;
// If mid point number is greater than the picked, then we need to check the smaller half for the picked number. Set the right pointer to mid pointer - 1
else if( guess(mid)==1) start=mid+1;
// If mid point number is less than the picked, then we need to check the larger half for the picked number. Set the left pointer to mid pointer + 1
else end=mid-1;
}
return -1;
}
}
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 values with each check.O(1)
because we only need two pointers to do the job.