core practice · coding problem
Longest substring without repeats
Find the length of the longest substring with no repeating characters using a variable-size sliding window.
The mechanism
A sliding window maintains a contiguous range and an aggregate (sum, count, max) as it moves. Expand the right edge to include elements; shrink the left edge when a constraint breaks. Each element enters and leaves the window at most once — O(n).
Expected evidence
lengthOfLongestSubstring('abcabcbb') -> 3Try this drill in the practice workspace →
Hints when you need them
- Expand the right edge; on a duplicate, shrink the left edge until the window is valid again.
- Track the last index of each character.
Watch for these mistakes
- Recomputing the aggregate from scratch instead of updating incrementally
- Shrinking with an `if` when a `while` is needed to restore the invariant
- Confusing fixed-size and variable-size window templates