Skip to main content

3. Longest Substring Without Repeating Characters

mediumAsked at Bloomberg

Given a string, find the length of the longest substring without repeating characters. Bloomberg uses this as their canonical sliding-window question — they want a clean O(n) two-pointer with a hash map storing last-seen indices.

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

Source citations

Public interview reports confirming this problem appears in Bloomberg loops.

  • Glassdoor (2026-Q1)Bloomberg SWE onsite reports list this as the most-asked sliding-window question.
  • Blind (2025-11)Bloomberg new-grad reports note this for nearly every onsite loop.

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 the length of 3.

Example 2

Input
s = "bbbbb"
Output
1

Explanation: The answer is "b", with the length of 1.

Example 3

Input
s = "pwwkew"
Output
3

Explanation: The answer is "wke", with the length of 3. Note that the answer must be a substring, not a subsequence.

Approaches

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

Track a window with left/right pointers. Map char -> last index. On a repeat, jump left to lastIndex + 1 (only if it moves forward).

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 ch = s[right];
    if (lastSeen.has(ch) && lastSeen.get(ch) >= left) {
      left = lastSeen.get(ch) + 1;
    }
    lastSeen.set(ch, right);
    if (right - left + 1 > best) best = right - left + 1;
  }
  return best;
}

Tradeoff: The canonical answer. Single pass; left jumps directly to lastIndex+1 (not one-at-a-time shrink). Bloomberg interviewers explicitly grade for the jump-vs-shrink choice.

2. Sliding window with set (one-step shrink)

Expand right; on conflict, shrink left until ch is gone from the window.

Time
O(2n)
Space
O(min(n, alphabet))
function lengthOfLongestSubstringSet(s) {
  const set = new Set();
  let left = 0;
  let best = 0;
  for (let right = 0; right < s.length; right++) {
    while (set.has(s[right])) {
      set.delete(s[left]);
      left++;
    }
    set.add(s[right]);
    if (right - left + 1 > best) best = right - left + 1;
  }
  return best;
}

Tradeoff: Easier to write under stress. Still amortized O(n) total work but each char can be visited twice. Use this if you blank on the jump trick.

Bloomberg-specific tips

Bloomberg interviewers grade specifically on the JUMP — left = max(left, lastSeen + 1). State 'when I hit a repeat, I jump left directly past the last occurrence' before coding. The max guard prevents left from moving backwards on a stale entry.

Common mistakes

  • Forgetting the max guard — left can jump BACKWARD if the last-seen index is before the current window.
  • Returning the substring instead of its length.
  • Off-by-one on window length — it's right - left + 1, not right - left.

Follow-up questions

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

  • Longest Substring with At Most K Distinct Characters (LC 340) — count distinct in the window.
  • Longest Repeating Character Replacement (LC 424) — track most-frequent char in the window.
  • Minimum Window Substring (LC 76) — sliding window over need/have counts.

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 the max guard on left?

If a char appeared LONG ago (before current left), lastSeen would push left backward and shrink the window incorrectly. Always: left = max(left, lastSeen + 1).

Is the alphabet bounded?

Per the problem, ASCII printable. Map size never exceeds 128. So 'O(min(n, 128))' but most authors call this O(n) space and move on.

Free learning resources

Curated free links for this problem.

Practice these live with InterviewChamp.AI

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

Practice these live with InterviewChamp.AI →