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: nums = [-1,0,3,5,9,12], target = 9
Output: 4
Explanation: 9 exists in nums and its index is 4
Input: nums = [-1,0,3,5,9,12], target = 2
Output: -1
Explanation: 2 does not exist in nums so return -1
EDGE CASE
Input: nums = [-1], target = 1
Output: 0
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 we find our number or exhaust our list.
1. Initialize left and right pointers
2. While left pointer is less than right pointer we have not exhausted the num list
a. Get the mid point of the two pointers
b. Check if mid point is less than, greater than, or equal to target
i.if mid point is less than target, then we know everything to the left of mid point can be eliminated from search
ii. if mid point is greater than target, then we know everything to the right of mid point can be eliminated from search
iii. otherwise the number is equal, so we return the mid index
3) The left pointer is greater than the right pointers, we have exhausted the num list, we cannot find the target, return -1
⚠️ Common Mistakes
Implement the code to solve the algorithm.
class Solution:
def search(self, nums: List[int], target: int) -> int:
# Initialize left and right pointers
l, r = 0, len(nums) - 1
# While left pointer is less than right pointer we have not exhausted the num list
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 target
# if mid point is less than target, then we know everything to the left of mid point can be eliminated from search
if nums[mid] < target:
l = mid + 1
# else if mid point is greater than target, then we know everything to the right of mid point can be eliminated from search
elif nums[mid] > target:
r = mid - 1
# else the number is equal, so we return the mid index
else:
return mid
# The left pointer is greater than the right pointers, we have exhausted the num list, return -1
return -1
class Solution {
public int search(int[] nums, int target) {
// Initialize left and right pointers
int low = 0;
int high = nums.length - 1;
// While left pointer is less than right pointer we have not exhausted the num list
while(low <= high){
// Get the mid point of the two pointers
int mid = (low + high) / 2;
// Check if mid point is less than, greater than, or equal to target
// the number is equal, so we return the mid index
if(nums[mid] == target) return mid;
// mid point is less than target, then we know everything to the left of mid point can be eliminated from search
else if(target > nums[mid]) low = mid + 1;
// mid point is greater than target, then we know everything to the right of mid point can be eliminated from search
else high = mid - 1;
}
// The left pointer is greater than the right pointers, we have exhausted the num list, return -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 numbers with each check.O(1)
because we only need two pointers to do the job.