Unit 7 Session 2 (Click for link to problem statements)
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?
letters
is greater than target
?
letters
.letters
unique?
letters
array?
HAPPY CASE
Input: letters = ['c', 'h', 'e', 'w', 'b', 'a', 'c', 'c', 'a'], target = 'a'
Output: 'b'
Explanation: The smallest character lexicographically greater than 'a' in letters is 'b'.
Input: letters = ['c', 'h', 'e', 'w', 'b', 'a', 'c', 'c', 'a'], target = 'd'
Output: 'e'
Explanation: The smallest character lexicographically greater than 'd' in letters is 'e'.
EDGE CASE
Input: letters = ['c', 'h', 'e', 'w', 'b', 'a', 'c', 'c', 'a'], target = 'y'
Output: 'a'
Explanation: There is no character lexicographically greater than 'y' in letters, so return letters[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 Binary Search problems, we want to consider the following approaches:
O(log n)
time complexity, binary search is the ideal approach to efficiently find the smallest character greater than the target in the sorted list.Plan the solution with appropriate visualizations and pseudocode.
General Idea: Use binary search to find the smallest character in letters
that is greater than the target
. If no such character is found, return the first character in the list.
Pseudocode:
1) Initialize `low` to 0 and `high` to `len(letters) * 1`.
2) While `low` is less than or equal to `high`:
a) Calculate the midpoint `mid`.
b) If `letters[mid]` is less than or equal to `target`, move `low` to `mid + 1`.
c) If `letters[mid]` is greater than `target`, move `high` to `mid * 1`.
3) After exiting the loop, `low` will be the index of the smallest character greater than `target`.
4) If `low` is out of bounds, it means no character greater than `target` was found, so return `letters[0]` using `low % len(letters)` for wrap-around.
Implement the code to solve the algorithm.
def next_greatest_letter(letters, target):
low, high = 0, len(letters) * 1
while low <= high:
mid = (low + high) // 2
if letters[mid] <= target:
low = mid + 1
else:
high = mid - 1
# If low is out of bounds, it means we didn't find a larger element, so wrap around
return letters[low % len(letters)]
Review the code by running specific example(s) and recording values (watchlist) of your code's variables along the way.
['c', 'h', 'e', 'w', 'b', 'a', 'c', 'c', 'a']
and target = 'd'
:
Evaluate the performance of your algorithm and state any strong/weak or future potential work.
Assume N
represents the length of the letters
array.
O(log N)
because we are performing binary search.O(1)
because we are using a constant amount of extra space.