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 = [4,5,6,7,0,1,2], target = 0
Output: 4
Input: nums = [4,5,6,7,0,1,2], target = 3
Output: -1
EDGE CASE
Input: nums = [1], target = 0
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: Let's split the array into two sorted arrays by finding the inflexion point using binary search. I.E. Find Minimum in Rotated Sorted Array. We can then run binary search on the two sorted arrays using binary search. I.E. Binary Search Problem
1. Create a function to get the inflexion point, the minimum value in list. For more details see [[Find Minimum in Rotated Sorted Array]]
2. Create/Run binary search for target in left and right half. For more details see [[Binary Search Problem]]
3. Return the search results. The max function checks between a positive value(found index) vs negative value(not found index).
⚠️ Common Mistakes
Implement the code to solve the algorithm.
class Solution:
def search(self, nums: List[int], target: int) -> int:
# Create/Use a function to get the inflexion point, the minimum value in list. For more details see [[Find Minimum in Rotated Sorted Array]]
minIndex = self.findMinIndex(nums)
# Create/Run binary search for target in left and right half. For more details see [[Binary Search Problem]]
checkLeftHalf = self.binarySearch(nums, 0, minIndex, target)
checkRightHalf = self.binarySearch(nums, minIndex, len(nums) - 1, target)
# Return the search results. The max function checks between a positive value(found index) vs negative value(not found index).
return max(checkLeftHalf, checkRightHalf)
# Create/Run binary search for target in left and right half. For more details see [[Binary Search Problem]]
def binarySearch(self, nums: List[int], left: int, right: int, target: int) -> int:
l, r = left, right
while l <= r:
mid = (l+r) // 2
if nums[mid] == target:
return mid
elif nums[mid] < target:
l = mid + 1
else:
r = mid - 1
return -1
# Create/Use a function to get the inflexion point, the minimum value in list. For more details see [[Find Minimum in Rotated Sorted Array]]
def findMinIndex(self, nums: List[int]) -> int:
l, r = 0, len(nums) - 1
while l < r:
mid = (l+r) // 2
if nums[mid] > nums[r]:
l = mid + 1
else:
r = mid
return r
class Solution {
public int search(int[] nums, int target) {
// Create/Use a function to get the inflexion point, the minimum value in list. For more details see [[Find Minimum in Rotated Sorted Array]]
int pivot = findPivot(nums);
// Create/Run binary search for target in left and right half. For more details see [[Binary Search Problem]]
return binarySearch(nums, 0, pivot, target) + binarySearch(nums, pivot + 1, nums.length - 1, target) + 1;
}
// Create/Use a function to get the inflexion point, the minimum value in list. For more details see [[Find Minimum in Rotated Sorted Array]]
public int findPivot(int nums[]){
int i = 0, j = nums.length - 1;
while(i < j - 1){
int mid = i + (j - i) / 2;
if(nums[i] < nums[mid] && nums[j] < nums[mid]){
i = mid;
}else {
j = mid;
}
}
return i;
}
// Create/Run binary search for target in left and right half. For more details see [[Binary Search Problem]]
public int binarySearch(int a[], int start, int end, int key){
int i = start, j = end;
while(i <= j){
int mid = i + (j - i) / 2;
if(a[mid] > key){
j = mid - 1;
}else if(a[mid] < key){
i = mid + 1;
}
else return mid;
}
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 used binary search for the inflexion point, then binary search on both sorted halves. O(1)
because we only need two pointers to do the job.