Determine Length of Longest Unique Substring
Company: Amazon
Role: Data Scientist
Category: Coding & Algorithms
Difficulty: Medium
Interview Round: Technical Screen
Quick Answer: This question evaluates string-processing skills and algorithmic problem-solving, including handling character uniqueness and performance considerations. Commonly asked in Coding & Algorithms interviews to assess a candidate's ability to implement efficient solutions and reason about time/space trade-offs, it targets practical application rather than purely conceptual understanding.
Constraints
- 0 <= len(s) <= 5 * 10^4
- s consists of English letters, digits, symbols, and spaces.
- The empty string returns 0.
Examples
Input: ("abcabcbb",)
Expected Output: 3
Explanation: The longest substring without repeats is "abc", length 3.
Input: ("bbbbb",)
Expected Output: 1
Explanation: Every character is the same, so the best is a single "b".
Input: ("pwwkew",)
Expected Output: 3
Explanation: "wke" has length 3; "pwke" is a subsequence, not a substring.
Input: ("",)
Expected Output: 0
Explanation: An empty string has no characters, so the length is 0.
Input: ("a",)
Expected Output: 1
Explanation: A single character is trivially unique.
Input: ("dvdf",)
Expected Output: 3
Explanation: The window jumps past the first 'd' to give "vdf", length 3.
Input: ("abba",)
Expected Output: 2
Explanation: After reaching the second 'a', start must not move backward; "ab" and "ba" each give length 2.
Input: (" ",)
Expected Output: 1
Explanation: A single space counts as a character, length 1.
Input: ("tmmzuxt",)
Expected Output: 5
Explanation: "mzuxt" is the longest unique window, length 5.
Input: ("abcdefg",)
Expected Output: 7
Explanation: All characters distinct, so the whole string qualifies.
Hints
- Use a sliding window: keep a left pointer `start` and scan with a right pointer.
- Store the most recent index of each character in a hash map.
- When you see a character already inside the current window (its last index >= start), jump `start` to just past that previous occurrence instead of shrinking one step at a time.
- Track the maximum window length (right - start + 1) as you go.