3. Longest Substring Without Repeating Characters
mediumAsked at AkamaiFind the length of the longest substring with all unique characters. Akamai connects this directly to sliding-window analysis of edge-server request logs — the same technique detects the longest burst of distinct client IPs in a time window without repeated connections.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in Akamai loops.
- Glassdoor (2026-Q1)— Akamai candidates report this as a high-frequency sliding window question in second-round interviews.
- Blind (2025-11)— Akamai thread lists this problem as representative of the sliding window category asked in CDN infra interviews.
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"3Explanation: The answer is 'abc', with the length of 3.
Example 2
s = "bbbbb"1Explanation: The answer is 'b', with the length of 1.
Example 3
s = "pwwkew"3Explanation: The answer is 'wke', with the length of 3.
Approaches
1. Sliding window with Map
Use a left pointer and a Map storing each character's most recent index. When a character repeats, jump the left pointer past its previous occurrence. Track the max window length throughout.
- Time
- O(n)
- Space
- O(min(n, alphabet))
function lengthOfLongestSubstring(s) {
const lastSeen = new Map(); // char → last seen index
let left = 0;
let maxLen = 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; // shrink window past duplicate
}
lastSeen.set(ch, right);
maxLen = Math.max(maxLen, right - left + 1);
}
return maxLen;
}Tradeoff: O(n) time, O(1) space in practice (bounded by character set size). The guard lastSeen.get(ch) >= left is critical — without it, a stale index from outside the current window would incorrectly shrink left.
2. Sliding window with Set
Use a Set to track characters in the current window. When a duplicate is encountered, shrink from the left, removing characters from the Set until the duplicate is gone.
- Time
- O(n)
- Space
- O(min(n, alphabet))
function lengthOfLongestSubstring(s) {
const seen = new Set();
let left = 0;
let maxLen = 0;
for (let right = 0; right < s.length; right++) {
while (seen.has(s[right])) {
seen.delete(s[left]);
left++;
}
seen.add(s[right]);
maxLen = Math.max(maxLen, right - left + 1);
}
return maxLen;
}Tradeoff: Also O(n) amortized — each character is added and removed at most once. Slightly simpler to reason about but may do more iterations than the Map approach in worst case.
Akamai-specific tips
Lead with the sliding window template: 'I'll maintain a window [left, right] where all characters are unique. I expand right until I hit a repeat, then advance left to restore the invariant.' Akamai likes seeing the invariant named explicitly. The Map approach's stale-index guard is a differentiator — mention it to show you've thought through edge cases.
Common mistakes
- Missing the lastSeen.get(ch) >= left guard in the Map approach — a character seen before the current window incorrectly collapses the window.
- Resetting left to lastSeen.get(ch) instead of lastSeen.get(ch) + 1 — keeps the duplicate in the window.
- Not handling the empty string — both approaches return 0 correctly if the loop never runs, but verify explicitly.
Follow-up questions
An interviewer at Akamai may pivot to one of these next:
- Longest Substring with At Most K Distinct Characters (LC 340) — generalize the uniqueness constraint.
- Minimum Window Substring (LC 76) — find the smallest window containing all characters of a pattern.
- How does your approach change if the character set is Unicode (> 128 symbols)?
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
Why do we need lastSeen.get(ch) >= left?
The Map retains entries for characters that fell outside the window when left advanced. Without the check, a stale previous index would incorrectly push left backward (or to before it currently is), breaking the invariant.
Is the Set or Map approach faster in practice?
Map is typically faster because left jumps directly past the previous occurrence instead of incrementally shrinking. Both are O(n) amortized.
Practice these live with InterviewChamp.AI
Drill Longest Substring Without Repeating Characters and other Akamai interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →