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 = [3,4,5,1,2]
Output: 1
Explanation: The original array was [1,2,3,4,5] rotated 3 times.
Input: nums = [4,5,6,7,0,1,2]
Output: 0
Explanation: The original array was [0,1,2,4,5,6,7] and it was rotated 4 times.
EDGE CASE
Input: nums = [11,13,15,17]
Output: 11
Explanation: The original array was [11,13,15,17] and it was rotated 4 times.
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 distinguish between the smaller and larger half, with each iteration until we select the smaller half and exhaust our possiblities. Binary Search
1. Initialize left and right pointers
2. While left pointer is less than 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 or greater than the right pointer
i. If mid point is greater than the right pointer, then the smaller half is the right half. Set the left pointer to mid pointer + 1.
ii. If mid pointer is less than the right pointer, then the smaller half is the left half. Set the right pointer to the mid pointer
3. Return number at right pointer for smallest number in list
⚠️ Common Mistakes
Implement the code to solve the algorithm.
class Solution:
def findMin(self, nums: List[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 possible numbers
while l < r:
# Get the mid point of the two pointers
mid = (l + r) // 2
# Check if mid point is less than or greater than the right pointer
if nums[mid] > nums[r]:
# If mid point is greater than the right pointer, then the smaller half is the right half. Set the left pointer to mid pointer + 1.
l = mid + 1
else:
# If mid pointer is less than the right pointer, then the smaller half is the left half. Set the right pointer to the mid pointer
r = mid
# Return number at right pointer for smallest number in list
return nums[r]
class Solution {
public int findMin(int[] nums) {
// Initialize left and right pointers
int l = 0;
int r = nums.length - 1;
// While left pointer is less than right pointer we have not exhausted the possible numbers
while (l < r) {
// Get the mid point of the two pointers
final int m = (l + r) / 2;
// Check if mid point is less than or greater than the right pointer
if (nums[m] < nums[r])
// mid pointer is less than the right pointer, then the smaller half is the left half. Set the right pointer to the mid pointer
r = m;
else
// mid point is greater than the right pointer, then the smaller half is the right half. Set the left pointer to mid pointer + 1.
l = m + 1;
}
// Return number at right pointer for smallest number in list
return nums[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 values with each check.O(1)
because we only need two pointers to do the job.