32. Longest Substring Without Repeating Characters
mediumAsked at RedditFind the longest substring without repeating characters. Reddit asks this to test sliding-window technique — the same pattern used to find the longest run of non-duplicate IPs hitting an endpoint (a fraud-detection primitive).
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in Reddit loops.
- Glassdoor (2026-Q1)— Reddit phone-screen sliding-window canonical.
- LeetCode Discuss (2025-11)— Reported as Reddit trust-and-safety warm-up.
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: "abc"
Example 2
s = "bbbbb"1Example 3
s = "pwwkew"3Explanation: "wke"
Approaches
1. Brute force all substrings
Check every substring for uniqueness.
- Time
- O(n^3)
- Space
- O(min(n, alphabet))
function lengthOfLongestSubstring(s) {
let best = 0;
for (let i = 0; i < s.length; i++) {
for (let j = i; j < s.length; j++) {
const set = new Set(s.slice(i, j + 1));
if (set.size === j - i + 1) best = Math.max(best, j - i + 1);
}
}
return best;
}Tradeoff: Cubic. Mention only as the anti-pattern.
2. Sliding window with hash map (optimal)
Maintain a window [l, r] of unique chars. When you see a duplicate at index i, jump l to max(l, lastSeen[c] + 1).
- Time
- O(n)
- Space
- O(min(n, alphabet))
function lengthOfLongestSubstring(s) {
const lastSeen = new Map();
let l = 0, best = 0;
for (let r = 0; r < s.length; r++) {
const c = s[r];
if (lastSeen.has(c) && lastSeen.get(c) >= l) {
l = lastSeen.get(c) + 1;
}
lastSeen.set(c, r);
best = Math.max(best, r - l + 1);
}
return best;
}Tradeoff: Linear single pass. The 'jump l forward' trick avoids the O(n) shift-by-one of naive window.
Reddit-specific tips
Reddit interviewers grade on the jump-l-forward trick (vs. shrinking one step at a time). Bonus signal: mention that the same pattern handles 'longest run of non-duplicate IPs' (a vote-fraud primitive) when keyed by IP instead of char.
Common mistakes
- Decrementing one step at a time — degrades to O(n^2).
- Forgetting the 'lastSeen.get(c) >= l' check (matters when char appeared before but outside the window).
- Computing window size as r - l (off by one).
Follow-up questions
An interviewer at Reddit may pivot to one of these next:
- Longest substring with at most k distinct chars (LC 340).
- Longest substring with at most 2 distinct chars (LC 159).
- Minimum window substring (LC 76).
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
Why use lastSeen index instead of a set?
Set lets you detect duplicates but not jump the left pointer to the right place — you'd have to shrink one at a time.
Could we use an int array of size 128?
Yes — bounded ASCII makes this even faster. Initialize to -1.
Practice these live with InterviewChamp.AI
Drill Longest Substring Without Repeating Characters and other Reddit interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →