Implement Python Function for Longest Unique Substring Length
Company: Amazon
Role: Data Scientist
Category: Coding & Algorithms
Difficulty: Medium
Interview Round: Onsite
Quick Answer: This question evaluates string manipulation and algorithmic problem-solving skills by requiring a Python implementation to determine the length of the longest substring without repeating characters, with an optional follow-up to extract all maximal substrings.
Longest Substring Without Repeating Characters — Length
Constraints
- 0 <= len(s) <= 5 * 10^4
- s consists of English letters, digits, symbols, and spaces.
- The empty string has answer 0.
- Substrings must be contiguous (not subsequences).
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; the best window is a single "b".
Input: ("pwwkew",)
Expected Output: 3
Explanation: "wke" (and "kew") have length 3; "pwke" is a subsequence, not a substring.
Input: ("",)
Expected Output: 0
Explanation: Empty string has no characters, so the answer is 0.
Input: ("a",)
Expected Output: 1
Explanation: A single character is trivially unique.
Input: ("dvdf",)
Expected Output: 3
Explanation: The window resets at the second 'd'; "vdf" has length 3.
Input: ("abba",)
Expected Output: 2
Explanation: After the repeated 'b', start jumps to index 2; "ab" at the end has length 2. The map must be guarded by start so the stale 'a' index does not shrink incorrectly.
Input: (" ",)
Expected Output: 1
Explanation: A single space is a valid unique character.
Input: ("tmmzuxt",)
Expected Output: 5
Explanation: "mzuxt" has length 5; the leading repeated 'm' and trailing 't' bound the window.
Hints
- Maintain a sliding window [start, i] that always contains distinct characters.
- Store the most recent index of each character in a hash map.
- When the current character was last seen at index j >= start, move start to j + 1 instead of shrinking one step at a time.
- After updating the window for index i, the current length is i - start + 1; keep the running maximum.
Longest Substring Without Repeating Characters — All Maximal Substrings
Constraints
- 0 <= len(s) <= 5 * 10^4
- s consists of English letters, digits, symbols, and spaces.
- Return distinct substrings only (the same text appearing at different positions counts once).
- Output is sorted in ascending lexicographic order.
- For the empty string return an empty list.
Examples
Input: ("abcabcbb",)
Expected Output: ["abc", "bca", "cab"]
Explanation: Max length is 3; the three distinct length-3 windows are abc, bca, cab.
Input: ("bbbbb",)
Expected Output: ["b"]
Explanation: Max length is 1 and the only distinct window is "b".
Input: ("pwwkew",)
Expected Output: ["kew", "wke"]
Explanation: Max length is 3; both "wke" and "kew" qualify, sorted to kew, wke.
Input: ("",)
Expected Output: []
Explanation: Empty string yields no substrings.
Input: ("a",)
Expected Output: ["a"]
Explanation: Single character is the only maximal window.
Input: ("dvdf",)
Expected Output: ["vdf"]
Explanation: Max length is 3 and only "vdf" reaches it; "dvd" repeats 'd'.
Input: ("abcdef",)
Expected Output: ["abcdef"]
Explanation: The whole string is unique, so it is the single maximal substring.
Input: ("aabbaabb",)
Expected Output: ["ab", "ba"]
Explanation: Max length is 2; the distinct length-2 unique windows are "ab" and "ba" (each occurs multiple times but is counted once).
Input: ("tmmzuxt",)
Expected Output: ["mzuxt"]
Explanation: Max length is 5 and only "mzuxt" achieves it.
Hints
- Run the same sliding window as the length problem, but also remember the window text whenever its length ties the current maximum.
- When you find a strictly longer window, reset your collection to just that new window.
- The same substring text can appear at multiple start positions, so de-duplicate with a set before returning.
- Sort the final list so the output is deterministic regardless of scan order.