18. Longest Substring Without Repeating Characters
mediumAsked at GlassdoorExtracting the longest unique-word run from a review snippet is analogous to what Glassdoor's NLP team does daily — this sliding-window problem tests whether you can maintain a dynamic window without backtracking on every character.
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.
Constraints
0 <= s.length <= 5 * 10^4s consists of English letters, digits, symbols and spaces
Examples
Example 1
s = "abcabcbb"3Explanation: "abc" is the longest substring without repeating characters, with length 3.
Example 2
s = "bbbbb"1Explanation: "b" is the only non-repeating substring.
Approaches
1. Brute force — check all substrings
Generate every substring and check for duplicates using a Set. O(n^3) — too slow beyond a few hundred characters.
- Time
- O(n^3)
- Space
- O(min(n, m))
function lengthOfLongestSubstring(s) {
let max = 0;
for (let i = 0; i < s.length; i++) {
for (let j = i + 1; j <= s.length; j++) {
const set = new Set(s.slice(i, j));
if (set.size === j - i) max = Math.max(max, j - i);
}
}
return max;
}Tradeoff:
2. Sliding window with hash map
Maintain a left pointer and a map of character → last-seen index. When a repeat is found, jump left past the previous occurrence. Single pass O(n).
- Time
- O(n)
- Space
- O(min(n, m))
function lengthOfLongestSubstring(s) {
const last = new Map();
let left = 0;
let max = 0;
for (let right = 0; right < s.length; right++) {
const c = s[right];
if (last.has(c) && last.get(c) >= left) {
left = last.get(c) + 1;
}
last.set(c, right);
max = Math.max(max, right - left + 1);
}
return max;
}Tradeoff:
Glassdoor-specific tips
Glassdoor interviewers use this problem to probe whether you can reason about two-pointer invariants without drawing them out. Emphasize that 'left' only ever moves forward — it never resets — which is the key insight that keeps this linear. They also like seeing you handle the edge case where the stored index of a repeated character is to the left of the current window, which is why the `>= left` guard matters.
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 Glassdoor interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →