15. Longest Substring Without Repeating Characters
mediumAsked at AdyenFind 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^4s consists of English letters, digits, symbols and spaces.
Examples
Example 1
s = "abcabcbb"3Example 2
s = "bbbbb"1Approaches
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.
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 →