Skip to main content

15. Longest Substring Without Repeating Characters

mediumAsked at Adyen

Find the length of the longest substring without repeating characters.

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

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

Example 2

Input
s = "bbbbb"
Output
1

Approaches

1. Brute force

Check every substring for uniqueness.

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

Tradeoff:

2. Sliding window with last-seen map

Jump left to the index after a duplicate's previous position.

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++) {
    if (last.has(s[r]) && last.get(s[r]) >= l) l = last.get(s[r]) + 1;
    last.set(s[r], r);
    best = Math.max(best, r - l + 1);
  }
  return best;
}

Tradeoff:

Adyen-specific tips

Adyen frames this as an idempotency-key replay window — they want the last-seen map so the left pointer jumps past duplicate keys without re-scanning.

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

Practice these live with InterviewChamp.AI →