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".
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".
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.