32. Longest Substring Without Repeating Characters
mediumAsked at SalesforceFind the length of the longest substring without repeating characters. Salesforce uses this as the canonical sliding-window introduction — they grade on left-pointer correctness.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in Salesforce loops.
- Glassdoor (2026-Q1)— Salesforce uses this as a baseline check for sliding-window comfort.
- Blind (2025-11)— Recurring on backend onsite loops.
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; track the longest without duplicates.
- Time
- O(n^3)
- Space
- O(min(n, k))
function lengthOfLongestSubstring(s) {
let best = 0;
for (let i = 0; i < s.length; i++) {
for (let j = i; j < s.length; j++) {
const sub = s.slice(i, j + 1);
if (new Set(sub).size === sub.length) best = Math.max(best, sub.length);
}
}
return best;
}Tradeoff: TLE on 5*10^4. Salesforce wants O(n).
2. Sliding window with last-seen map
Map last-seen index of each char. Maintain window [l, r]. When s[r] was seen at idx >= l, jump l to idx + 1.
- Time
- O(n)
- Space
- O(k) k = alphabet
function lengthOfLongestSubstring(s) {
const lastSeen = new Map();
let l = 0, best = 0;
for (let r = 0; r < s.length; r++) {
if (lastSeen.has(s[r]) && lastSeen.get(s[r]) >= l) {
l = lastSeen.get(s[r]) + 1;
}
lastSeen.set(s[r], r);
best = Math.max(best, r - l + 1);
}
return best;
}Tradeoff: Single pass. The `>= l` check is critical — earlier duplicates outside the window must be ignored.
Salesforce-specific tips
Salesforce explicitly tests the `lastSeen.get(c) >= l` clause — a common bug is using `if (lastSeen.has(c))` alone, which causes l to JUMP BACKWARD when the same char appears far outside the current window. Bonus signal: mention this 'don't backtrack' principle generalizes to all sliding-window problems.
Common mistakes
- Jumping l backward when an old duplicate appears — corrupts the window.
- Computing length as `r - l` instead of `r - l + 1` — off by one.
- Resetting the map on each iteration — wipes useful history.
Follow-up questions
An interviewer at Salesforce may pivot to one of these next:
- Longest Substring with At Most K Distinct (LC 340).
- Longest Repeating Character Replacement (LC 424).
- Minimum Window Substring (LC 76).
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
Why the `>= l` clause?
Because a duplicate from before the current window start shouldn't move l. Without the check, l could move backward, breaking the monotone-window invariant.
What if the alphabet is unicode-wide?
Map scales to O(unique chars). For dense ASCII you could use a 128-element array; for full Unicode, Map is the right choice.
Practice these live with InterviewChamp.AI
Drill Longest Substring Without Repeating Characters and other Salesforce interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →