Longest Substring Without Repeating Characters
How the interviewer asks it
“Given a string, return the length of the longest substring that contains no repeated characters.”
abcabcbb
3
In the practice room your code reads the input from standard input, and Run checks it against tests like this one. Try it now in your browser, in Python or JavaScript.
Typed drills are free and unlimited: the AI scores your answer against the outline below.
What the interviewer is looking for
- You name the sliding window pattern and explain why the window only ever moves forward.
- You store the last index of each character so the left edge can jump instead of crawling.
- You only move the left edge forward, never back, when a repeat was seen before the window.
- Clear O(n) reasoning and the size of the character set as the space bound.
How to structure your answer
- 01
Clarify
Ask about the character set (ASCII or Unicode) and whether the answer is a length or the substring.
- 02
Brute force
Check every substring for repeats: O(n³) or O(n²) with a set. Mention it and move on.
- 03
Sliding window
Walk right across the string. Keep a map from character to its last index. If the character was seen at or after left, move left to that index plus one. Update the best length with right − left + 1.
- 04
Test
"abcabcbb" gives 3, "bbbbb" gives 1, "abba" gives 2, which catches the left edge moving backwards.
- 05
Complexity
O(n) time, O(k) space for k distinct characters.
Best answers from candidates
No candidate answers yet. Type yours, and if it scores 6 or more you can show it here without your name.
Answers are shared by candidates, without names, after a privacy check. Add yours
Common mistakes
- Moving left to the last seen index even when that index is before the window (fails on "abba").
- Off-by-one errors in the window length.
- Clearing the whole set on every repeat, which restarts the scan and is O(n²).
Follow-up questions to expect
- Allow at most k distinct characters in the substring.
- Return the substring itself, and the first one if there is a tie.
Now answer it out loud
Max will ask you this exact question, listen to your answer, push on the weak spots with follow-ups like the ones above, and score you when you finish.
Practice with MaxFree: a 5-minute voice round and unlimited typed drills, with scored feedback. No card, and your camera is never recorded.
More coding interview questions
Preparing for this round? Read how to prepare for a coding interview: a four-week DSA plan.