Skip to main content

15. Longest Substring Without Repeating Characters

mediumAsked at Wise

Find the longest unique-character window in a string — Wise uses it as a stand-in for the longest streak of unmatched ledger entries in a reconciliation scan.

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

Problem

Given a string s, find the length of the longest substring that contains no repeating characters. Return the length, not the substring itself.

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

Try each (i,j) window and rescan for duplicates inside.

Time
O(n^3)
Space
O(min(n, alphabet))
let best=0;
for (let i=0;i<s.length;i++)
  for (let j=i;j<s.length;j++){
    if (new Set(s.slice(i,j+1)).size===j-i+1) best=Math.max(best,j-i+1);
  }
return best;

Tradeoff:

2. Sliding window with last-seen index

Right pointer expands; when a repeat is seen, jump left to one past the previous occurrence. Each char is visited at most twice.

Time
O(n)
Space
O(min(n, alphabet))
function lengthOfLongestSubstring(s){
  const lastSeen = new Map();
  let left = 0, 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:

Wise-specific tips

Wise grades the sliding-window pattern itself because their reconciliation jobs run the same shape over time-windowed transaction streams — the question lets them watch whether you instinctively avoid O(n^2) on a streaming primitive.

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 Wise interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.

Practice these live with InterviewChamp.AI →