Skip to main content

3. Longest Substring Without Repeating Characters

mediumAsked at Uber

Find the length of the longest substring of s with no repeating characters. Uber asks this as a sliding-window primer to see if you reach for a left/right pointer plus a hash set instead of regenerating substrings.

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

Source citations

Public interview reports confirming this problem appears in Uber loops.

  • Glassdoor (2026-Q1)Uber SWE phone-screen reports list this as a frequent 30-minute medium.
  • Blind (2025-12)Recurring in Uber L4/L5 onsite coding rounds.

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

Explanation: The answer is "wke". Note "pwke" is a subsequence, not a substring.

Approaches

1. Brute-force enumerate substrings

For every (i, j), check whether s[i..j] has only unique chars; track the max.

Time
O(n^3)
Space
O(min(n, alphabet))
function lengthOfLongestSubstringBrute(s) {
  function allUnique(t) {
    const seen = new Set();
    for (const c of t) { if (seen.has(c)) return false; seen.add(c); }
    return true;
  }
  let best = 0;
  for (let i = 0; i < s.length; i++) {
    for (let j = i; j < s.length; j++) {
      if (allUnique(s.slice(i, j + 1))) best = Math.max(best, j - i + 1);
    }
  }
  return best;
}

Tradeoff: Cubic — the uniqueness check is O(n) and there are O(n^2) substrings. Mention before pivoting to sliding window.

2. Sliding window with hash set (optimal)

Maintain [left, right] window where every char is unique. On a duplicate, advance left until the duplicate is dropped.

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

Tradeoff: Each char is added and removed at most once, so the total work is O(n). The set keeps the uniqueness invariant on the window.

3. Sliding window with index map (faster constant)

Store each char's last index. On a duplicate, jump left to just past the prior occurrence.

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

Tradeoff: Skips the inner while loop by jumping left directly. Same asymptotic complexity but better constants on long strings with many duplicates.

Uber-specific tips

Uber interviewers care that you name the sliding-window invariant out loud: 'I maintain a window [left, right] where every character is unique. When I see a duplicate at right, I advance left until the duplicate exits.' That sentence is worth more than any line of code at Uber's bar.

Common mistakes

  • Confusing substring with subsequence — 'pwke' is a subsequence of 'pwwkew' but not a substring.
  • Resetting the set on every duplicate (O(n^2)) instead of shrinking the window.
  • Updating left to last.get(c) instead of last.get(c) + 1 — off-by-one error.

Follow-up questions

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

  • Longest substring with at most K distinct characters (LC 340).
  • Longest repeating character replacement (LC 424).
  • What if the alphabet were Unicode (millions of code points)?

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 sliding window over re-scanning?

The window invariant lets each char enter and leave at most once over the entire pass, giving O(n) total work instead of the O(n^3) brute force.

Is the index-map version always better?

Same asymptotics. Index-map wins on strings with frequent duplicates (one jump vs many one-step advances). On strings with few duplicates the simpler set version is fine.

Free learning resources

Curated free links for this problem.

Practice these live with InterviewChamp.AI

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

Practice these live with InterviewChamp.AI →