Longest Substring Without Repeated Characters
Company: Cisco
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: easy
Interview Round: Technical Screen
## Problem
Implement `longest_unique_substring(s)`. Return the longest contiguous substring of `s` that contains no repeated character. If several substrings have the same maximum length, return the leftmost one. Return the empty string when `s` is empty.
## Constraints
- `0 <= len(s) <= 200,000`
- `s` contains ASCII characters.
- Character comparison is case-sensitive.
## Examples
- `"abcabcbb"` returns `"abc"`.
- `"bbbbb"` returns `"b"`.
- `"pwwkew"` returns `"wke"`.
- `""` returns `""`.
## Clarifications
The answer must be an actual substring, not only its length. The tie rule is based on the smallest starting index.
## Hint
Maintain a window with unique characters. When the next character repeats, move the left edge past the previous occurrence without moving it backward.
## Interview Follow-ups
- Return the start index and length without copying the substring.
- Adapt the method to a stream where old input cannot be reread.
- Explain the difference between character, byte, and grapheme handling for non-ASCII text.
Quick Answer: Return the leftmost longest contiguous substring whose characters are all unique. Handle empty input, ASCII and case-sensitive semantics, large strings, actual-substring output, streaming constraints, and the distinction among bytes, characters, and graphemes.
Implement `longest_unique_substring(s)`. Return the longest contiguous substring of `s` whose characters are all distinct. If multiple maximum-length substrings exist, return the leftmost one, meaning the one with the smallest starting index. Return the empty string when `s` is empty. Return the substring itself rather than only its length.
Constraints
- 0 <= len(s) <= 200,000
- s contains only ASCII characters.
- Character comparison is case-sensitive.
- When maximum-length substrings tie, return the one with the smallest starting index.
Examples
Input: ('',)
Expected Output: ''
Explanation: Empty input returns the empty string.
Input: ('z',)
Expected Output: 'z'
Explanation: A one-character input is already a unique substring.
Hints
- Maintain a window with unique characters. When the next character repeats, move the left edge past the previous occurrence without moving it backward.