Skip to main content

32. Longest Substring Without Repeating Characters

mediumAsked at Vercel

Find the length of the longest substring without repeating characters. Vercel asks this as their canonical sliding-window question — the same shape they use to find the longest run of unique cache-keys before invalidation.

By Alex Chen, Founder, InterviewChamp.AI · Last verified

Source citations

Public interview reports confirming this problem appears in Vercel loops.

  • Glassdoor (2025-Q4)Vercel platform onsite; sliding window expected.
  • Blind (2026-Q1)Mentioned in Vercel runtime engineer screen.

Problem

Given a string s, find the length of the longest substring without repeating characters.

Constraints

  • 0 <= s.length <= 5 * 10^4
  • s consists of English letters, digits, symbols and spaces.

Examples

Example 1

Input
s = "abcabcbb"
Output
3

Explanation: The answer is "abc", with length 3.

Example 2

Input
s = "bbbbb"
Output
1

Example 3

Input
s = "pwwkew"
Output
3

Approaches

1. Brute force enumerate substrings

For every (i, j), check if s[i..j] has duplicates.

Time
O(n^3)
Space
O(min(n, alphabet))
function lengthOfLongestSubstring(s) {
  let best = 0;
  for (let i = 0; i < s.length; i++) {
    for (let j = i; j < s.length; j++) {
      const set = new Set();
      let ok = true;
      for (let k = i; k <= j; k++) {
        if (set.has(s[k])) { ok = false; break; }
        set.add(s[k]);
      }
      if (ok) best = Math.max(best, j - i + 1);
    }
  }
  return best;
}

Tradeoff: Cubic. Mention only to motivate the sliding window.

2. Sliding window with last-seen map (optimal)

Walk right with j. Track the last index each char was seen. If s[j] was seen at index >= window-left, advance left to seen+1.

Time
O(n)
Space
O(min(n, alphabet))
function lengthOfLongestSubstring(s) {
  const lastSeen = new Map();
  let left = 0;
  let best = 0;
  for (let right = 0; right < s.length; right++) {
    const c = s[right];
    if (lastSeen.has(c) && lastSeen.get(c) >= left) {
      left = lastSeen.get(c) + 1;
    }
    lastSeen.set(c, right);
    best = Math.max(best, right - left + 1);
  }
  return best;
}

Tradeoff: Single pass, O(n). The `lastSeen.get(c) >= left` check is the trick — without it, an old index outside the current window would wrongly shrink it.

Vercel-specific tips

Vercel grades the sliding-window pattern. Bonus signal: the `lastSeen.get(c) >= left` guard (a duplicate outside the current window doesn't count) and using Map over plain object for the same reasons as LC 1.

Common mistakes

  • Advancing left only by 1 — works but degrades to O(n) per step in some pathological cases; jumping to seen+1 is faster.
  • Forgetting the `>= left` check — shrinks the window incorrectly on stale duplicates.
  • Resetting the map every iteration — defeats the optimization.

Follow-up questions

An interviewer at Vercel may pivot to one of these next:

  • Longest substring with at most k distinct characters (LC 340).
  • Longest substring with at most 2 distinct characters (LC 159).
  • Minimum Window Substring (LC 76, hard).

Solve it now

Free. No sign-up. Python and JavaScript run instantly in your browser.

Output

Press Run or Cmd+Enter to execute

FAQ

Why jump left to seen+1 instead of incrementing?

When you encounter a duplicate inside the window, you know exactly where the conflict is; jumping past it in one step is O(1) per advance. Incrementing one at a time would still work but in some patterns degrades to O(n) per character.

Why the `>= left` guard?

The map stores ALL last-seen indices, including positions before the current window-left. Without the guard, you'd shrink the window based on a stale duplicate that's already been left behind.

Practice these live with InterviewChamp.AI

Drill Longest Substring Without Repeating Characters and other Vercel interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.

Practice these live with InterviewChamp.AI →