Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on array fundamentals.
You are given an array of characters letters that is sorted in non-decreasing order, and a character target. There are at least two different characters in letters.
Return the smallest character in letters that is lexicographically greater than target. If such a character does not exist, return the first character in letters.
Example 1:
Input: letters = ["c","f","j"], target = "a" Output: "c" Explanation: The smallest character that is lexicographically greater than 'a' in letters is 'c'.
Example 2:
Input: letters = ["c","f","j"], target = "c" Output: "f" Explanation: The smallest character that is lexicographically greater than 'c' in letters is 'f'.
Example 3:
Input: letters = ["x","x","y","y"], target = "z" Output: "x" Explanation: There are no characters in letters that is lexicographically greater than 'z' so we return letters[0].
Constraints:
2 <= letters.length <= 104letters[i] is a lowercase English letter.letters is sorted in non-decreasing order.letters contains at least two different characters.target is a lowercase English letter.Problem summary: You are given an array of characters letters that is sorted in non-decreasing order, and a character target. There are at least two different characters in letters. Return the smallest character in letters that is lexicographically greater than target. If such a character does not exist, return the first character in letters.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Binary Search
["c","f","j"] "a"
["c","f","j"] "c"
["x","x","y","y"] "z"
count-elements-with-strictly-smaller-and-greater-elements)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #744: Find Smallest Letter Greater Than Target
class Solution {
public char nextGreatestLetter(char[] letters, char target) {
int i = Arrays.binarySearch(letters, (char) (target + 1));
i = i < 0 ? -i - 1 : i;
return letters[i % letters.length];
}
}
// Accepted solution for LeetCode #744: Find Smallest Letter Greater Than Target
func nextGreatestLetter(letters []byte, target byte) byte {
i := sort.Search(len(letters), func(i int) bool { return letters[i] > target })
return letters[i%len(letters)]
}
# Accepted solution for LeetCode #744: Find Smallest Letter Greater Than Target
class Solution:
def nextGreatestLetter(self, letters: List[str], target: str) -> str:
i = bisect_right(letters, ord(target), key=lambda c: ord(c))
return letters[i % len(letters)]
// Accepted solution for LeetCode #744: Find Smallest Letter Greater Than Target
impl Solution {
pub fn next_greatest_letter(letters: Vec<char>, target: char) -> char {
let mut l = 0;
let mut r = letters.len();
while l < r {
let mid = l + (r - l) / 2;
if letters[mid] > target {
r = mid;
} else {
l = mid + 1;
}
}
letters[l % letters.len()]
}
}
// Accepted solution for LeetCode #744: Find Smallest Letter Greater Than Target
function nextGreatestLetter(letters: string[], target: string): string {
const idx = _.sortedIndex(letters, target + '\0');
return letters[idx % letters.length];
}
Use this to step through a reusable interview workflow for this problem.
Check every element from left to right until we find the target or exhaust the array. Each comparison is O(1), and we may visit all n elements, giving O(n). No extra space needed.
Each comparison eliminates half the remaining search space. After k comparisons, the space is n/2ᵏ. We stop when the space is 1, so k = log₂ n. No extra memory needed — just two pointers (lo, hi).
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Setting `lo = mid` or `hi = mid` can stall and create an infinite loop.
Usually fails on: Two-element ranges never converge.
Fix: Use `lo = mid + 1` or `hi = mid - 1` where appropriate.