Skip to main content

12. Longest Substring Without Repeating Characters

mediumAsked at ByteDance

Find 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^4
  • s consists of English letters, digits, symbols and spaces

Examples

Example 1

Input
s = "abcabcbb"
Output
3

Example 2

Input
s = "bbbbb"
Output
1

Approaches

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 length

Tradeoff:

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.

Output

Press Run or Cmd+Enter to execute

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 →