Find longest unique-character substring
Company: Snowflake
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: Medium
Interview Round: Onsite
Quick Answer: This question evaluates proficiency in string manipulation, use of associative data structures (hash maps/sets), and algorithmic thinking for detecting and maintaining unique-character substrings.
Constraints
- 0 <= len(s) <= 5 * 10^4
- s may contain letters, digits, symbols, and spaces (any Unicode code points)
- Return 0 for the empty string
Examples
Input: ("abcabcbb",)
Expected Output: 3
Explanation: The answer is "abc", length 3.
Input: ("bbbbb",)
Expected Output: 1
Explanation: The answer is "b", length 1 — every character repeats.
Input: ("pwwkew",)
Expected Output: 3
Explanation: The answer is "wke", length 3. Note "pwke" is a subsequence, not a substring.
Input: ("",)
Expected Output: 0
Explanation: Empty string has no substring; length 0.
Input: ("a",)
Expected Output: 1
Explanation: Single character, length 1.
Input: ("dvdf",)
Expected Output: 3
Explanation: The answer is "vdf", length 3. The repeated 'd' jumps start to index 2, and the window correctly grows again.
Input: ("abba",)
Expected Output: 2
Explanation: After "ab", the second 'b' moves start to 2; the trailing 'a' must NOT move start backward (its old index 0 is outside the window), so "ab" and "ba" both give length 2.
Input: (" ",)
Expected Output: 1
Explanation: A single space is a valid non-repeating character, length 1.
Input: ("tmmzuxt",)
Expected Output: 5
Explanation: The answer is "mzuxt", length 5. The leading 't' at index 0 must not shrink the window when the final 't' appears, since index 0 is already outside it.
Input: ("abcdefghijklmnopqrstuvwxyz",)
Expected Output: 26
Explanation: All 26 letters are distinct, so the whole string is the answer, length 26.
Hints
- Use two pointers forming a window [start, i]; expand i one step at a time and shrink from the left only when you hit a repeat.
- Store the last index at which each character was seen so you can jump start directly past the previous occurrence instead of stepping one-by-one.
- Guard the jump with `last_seen[ch] >= start` — a stale occurrence that is already outside the window (e.g. the second 'a' in "abba") must not move start backward.