17. Longest Substring Without Repeating Characters
mediumAsked at IndeedFind the length of the longest substring without repeating characters — the sliding window pattern powers Indeed's query tokenizer deduplication.
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 integer length.
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 with a Set to detect duplicates.
- Time
- O(n^2)
- Space
- O(min(n,m))
function lengthOfLongestSubstring(s) {
let max = 0;
for (let i = 0; i < s.length; i++) {
const seen = new Set();
for (let j = i; j < s.length; j++) {
if (seen.has(s[j])) break;
seen.add(s[j]);
max = Math.max(max, j - i + 1);
}
}
return max;
}Tradeoff:
2. Sliding window with last-seen map
Track the last index of each character; jump the left pointer directly past the duplicate to avoid inner-loop scans.
- Time
- O(n)
- Space
- O(min(n,m))
function lengthOfLongestSubstring(s) {
const lastIndex = new Map();
let max = 0;
let left = 0;
for (let right = 0; right < s.length; right++) {
const c = s[right];
if (lastIndex.has(c) && lastIndex.get(c) >= left) {
left = lastIndex.get(c) + 1;
}
lastIndex.set(c, right);
max = Math.max(max, right - left + 1);
}
return max;
}Tradeoff:
Indeed-specific tips
Indeed's search team frequently asks this; connect the window technique to streaming token deduplication in their job-title indexer to demonstrate domain awareness.
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 Indeed interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →