3. Longest Substring Without Repeating Characters
mediumAsked at BroadcomFind the length of the longest substring with all unique characters using a sliding window. Broadcom asks this to verify sliding-window intuition — the same technique underlies congestion window management in TCP implementations embedded in Broadcom's network silicon.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in Broadcom loops.
- Glassdoor (2026-Q1)— Cited as a top-frequency problem in Broadcom SWE onsite coding rounds across multiple role levels.
- Blind (2025-11)— Broadcom threads consistently list this as a must-prepare sliding-window problem for infrastructure software 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 length 3.
Example 2
s = "bbbbb"1Explanation: The answer is 'b' with length 1.
Example 3
s = "pwwkew"3Explanation: The answer is 'wke' with length 3.
Approaches
1. Sliding window with Set
Maintain a window [left, right] with a Set of characters in the window. If s[right] is already in the Set, shrink from the left until it's removed, then expand right.
- Time
- O(n)
- Space
- O(min(n, alphabet))
function lengthOfLongestSubstring(s) {
const charSet = new Set();
let left = 0, maxLen = 0;
for (let right = 0; right < s.length; right++) {
while (charSet.has(s[right])) {
charSet.delete(s[left]);
left++;
}
charSet.add(s[right]);
maxLen = Math.max(maxLen, right - left + 1);
}
return maxLen;
}Tradeoff: O(n) amortised — each character is added and removed at most once. Simple to reason about correctness.
2. Sliding window with Map (jump optimisation)
Store the last seen index of each character. When a duplicate is found, jump left directly to max(left, lastSeen + 1) instead of shrinking one at a time.
- Time
- O(n)
- Space
- O(min(n, alphabet))
function lengthOfLongestSubstring(s) {
const lastSeen = new Map();
let left = 0, maxLen = 0;
for (let right = 0; right < s.length; right++) {
if (lastSeen.has(s[right]) && lastSeen.get(s[right]) >= left) {
left = lastSeen.get(s[right]) + 1;
}
lastSeen.set(s[right], right);
maxLen = Math.max(maxLen, right - left + 1);
}
return maxLen;
}Tradeoff: Still O(n) but avoids repeated Set deletions by jumping left directly. Preferred by Broadcom interviewers who appreciate the constant-factor improvement and the use of index tracking.
Broadcom-specific tips
Present both approaches: 'The Set version is easy to verify correct; the Map version avoids the inner while loop by jumping left directly.' Broadcom interviewers from the infrastructure software side frequently follow up by asking how the window size correlates to buffer-sizing decisions — drawing that analogy shows networking domain awareness. For the Map version, don't forget the guard left = max(left, lastSeen + 1) to prevent the left pointer from moving backwards when a character was seen before the current window.
Common mistakes
- In the Map approach, not guarding against left moving backwards when lastSeen[char] < left.
- Forgetting to handle an empty string — return 0 immediately.
- Computing window length as right − left instead of right − left + 1.
- Using character codes instead of characters for the Map key — fine in C++ but unnecessary in JavaScript and error-prone.
Follow-up questions
An interviewer at Broadcom may pivot to one of these next:
- Longest substring with at most k distinct characters (LC 340).
- Minimum window substring (LC 76) — harder sliding window with target frequency counts.
- How would you solve this for a stream where characters arrive one at a time?
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
Why is the Map approach faster in practice even though both are O(n)?
The Set version may shrink the window character by character in the worst case (O(n) inner loop per step), while the Map version always jumps left in O(1). The total work is still O(n) for both, but the constant factor differs.
What is 'min(n, alphabet)' space?
The map/set holds at most one entry per unique character. The alphabet size bounds this — for ASCII it's 128, for Unicode it could be up to n in the worst case.
Does this problem require UTF-16 awareness in JavaScript?
For surrogate pairs (emoji, some CJK), s[i] may not be a full code point. In production code use Array.from(s) to iterate code points; in interviews, ASCII input is assumed.
Practice these live with InterviewChamp.AI
Drill Longest Substring Without Repeating Characters and other Broadcom interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →