mediumCodingEntryMidSenior
Find the length of the longest substring without repeating characters.
2 min readUpdated 2026-08-15
Answer
Short answer
Maintain a sliding window with a map from character to its last seen index; when a duplicate appears inside the current window, jump the left boundary past that previous occurrence rather than shrinking one step at a time, giving a single-pass O(n) solution.
Was this answer helpful?
Likely follow-ups
- How would you return the substring itself rather than its length?
- What changes if the input is a Unicode string with surrogate pairs?
- How would you generalise this to at most k distinct characters?
- Can you achieve O(1) extra space, and under what assumption?
Related questions
- Detect whether a linked list has a cycle, and return the node where the cycle begins.mediumAlso on two-pointers4 min
- Group a list of words so that words which are anagrams of each other end up together. What is your key, and what does it cost?mediumAlso on hashing5 min
- Remove the nth node from the end of a singly linked list in a single pass.mediumAlso on two-pointers4 min
- Return every distinct triplet in the array that sums to zero.mediumAlso on two-pointers4 min