Smallest Letter Greater Than Target - codepath/compsci_guides GitHub Wiki

Unit 7 Session 2 (Click for link to problem statements)

Problem Highlights

  • 💡 Difficulty: Medium
  • Time to complete: 20 mins
  • 🛠️ Topics: Binary Search, Lexicographic Order

1: U-nderstand

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?
  • What should be returned if no character in letters is greater than target?
    • Return the first character in letters.
  • Are all the characters in letters unique?
    • The problem does not specify, so assume there could be duplicates.
  • What is the size of the letters array?
    • The problem does not specify, but assume it could be large.
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].

2: M-atch

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:

  • Binary Search: Since the problem requires 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.

3: P-lan

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.

Binary Search Implementation

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.

⚠️ Common Mistakes

  • Forgetting to handle the case where no character in the list is greater than the target, which requires wrapping around to the start of the list.

4: I-mplement

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)]

5: R-eview

Review the code by running specific example(s) and recording values (watchlist) of your code's variables along the way.

  • Trace through your code with the input ['c', 'h', 'e', 'w', 'b', 'a', 'c', 'c', 'a'] and target = 'd':
    • The binary search should correctly identify that 'e' is the smallest character greater than 'd' and return it.

6: E-valuate

Evaluate the performance of your algorithm and state any strong/weak or future potential work.

Assume N represents the length of the letters array.

  • Time Complexity: O(log N) because we are performing binary search.
  • Space Complexity: O(1) because we are using a constant amount of extra space.