12. Longest Substring Without Repeating Characters
mediumAsked at ByteDanceFind the longest substring with no repeating characters — ByteDance uses it to gauge sliding-window fluency before scaling to user-session deduplication.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Problem
Given a string s, return the length of the longest substring that contains no repeated characters. The substring must be contiguous.
Constraints
0 <= s.length <= 5 * 10^4s consists of English letters, digits, symbols and spaces
Examples
Example 1
s = "abcabcbb"3Example 2
s = "bbbbb"1Approaches
1. Check every substring
Generate all substrings and check uniqueness for each.
- Time
- O(n^3)
- Space
- O(n)
// enumerate substrings, build a Set per substring, track max lengthTradeoff:
2. Sliding window with last-seen map
Maintain a window [l, r] of unique characters. When you see a repeat inside the window, jump l past its previous index.
- Time
- O(n)
- Space
- O(min(n, alphabet))
function lengthOfLongestSubstring(s) {
const last = new Map();
let l = 0, best = 0;
for (let r = 0; r < s.length; r++) {
const c = s[r];
if (last.has(c) && last.get(c) >= l) l = last.get(c) + 1;
last.set(c, r);
best = Math.max(best, r - l + 1);
}
return best;
}Tradeoff:
ByteDance-specific tips
ByteDance recruiters love when you connect the window-jump trick to how their session-tracker dedupes repeated taps in a TikTok scroll within a short interval.
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
Practice these live with InterviewChamp.AI
Drill Longest Substring Without Repeating Characters and other ByteDance interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →