Find longest substring without repeating characters
Company: Microsoft
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Quick Answer: This question evaluates a candidate's skills in string manipulation, algorithm design, and reasoning about time and space complexity when identifying maximal contiguous substrings with distinct characters.
Constraints
- 0 <= len(s) <= 2 * 10^5
- s may contain letters, digits, symbols, and spaces.
- The answer substring must be contiguous, not a subsequence.
Examples
Input: ("abcabcbb",)
Expected Output: 3
Explanation: The longest distinct-char window is "abc" with length 3.
Input: ("bbbbb",)
Expected Output: 1
Explanation: Every character is 'b', so the best window is a single "b".
Hints
- Use a sliding window with two pointers [start, i]. Expand i one character at a time and shrink the window from the left only when a duplicate enters.
- Track the last index at which each character was seen. When you hit a repeat, jump `start` to one past the previous occurrence — but never move `start` backward (guard with `last_seen[ch] >= start`).
- At each step the current window length is `i - start + 1`; keep the running maximum.