By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.
(A Complete FAANG-Level Interview Guide)
"This problem appears in 1 out of every 3 onsite interviews—master it, and you’ll prove you can handle sliding windows, edge cases, and optimal trade-offs under pressure."
Before tackling this problem, ensure you understand: 1. Hash Maps (Dictionaries) – For O(1) lookups and storing character indices. 2. Sliding Window Technique – Maintaining a dynamic window of valid elements. 3. Two-Pointer Technique – Expanding and contracting a window efficiently.
left=0, right=0; while right < n: expand window, adjust left if invalid.
last_seen = {}
left
if not s: return 0
if n - left <= max_len: break
(Repeatable mental checklist for every sliding window problem.)
What variables track the window? (left, right pointers.)
right
Initialize Tracking Structures
Use a hash map (last_seen) to store the most recent index of each character.
last_seen
Expand the Window (Right Pointer)
0
n-1
For each char = s[right], check if it’s already in the window.
char = s[right]
Adjust the Window (Left Pointer)
char
last_seen[char] >= left
last_seen[char] + 1
Update last_seen[char] = right.
last_seen[char] = right
Update Maximum Length
After each adjustment, compute max_len = max(max_len, right - left + 1).
max_len = max(max_len, right - left + 1)
Edge Cases
n
All same chars → return 1.
1
Optimize (Optional)
n - left <= max_len
Problem: Given "abcabcbb", return 3 (substring "abc").
"abcabcbb"
3
"abc"
left = 0
max_len = 0
7
right=0
'a'
max_len=1
right=1
'b'
max_len=2
right=2
'c'
max_len=3
right=3
left=1
last_seen['a']=3
"bca"
right=7
def lengthOfLongestSubstring(s: str) -> int: last_seen = {} left = max_len = 0 for right, char in enumerate(s): if char in last_seen and last_seen[char] >= left: left = last_seen[char] + 1 last_seen[char] = right max_len = max(max_len, right - left + 1) return max_len
Time Complexity: O(n) (single pass). Space Complexity: O(min(m, n)) (m = character set size, e.g., 26 for lowercase letters).
Why This Works: - The sliding window ensures we only consider valid substrings. - last_seen tracks the most recent duplicate, allowing left to jump directly to the correct position.
Problem: Given "abba", return 2 (substring "ab" or "ba").
"abba"
2
"ab"
"ba"
left=2
last_seen['b'] + 1
last_seen['a']=0 < left
(Same as above—handles duplicates automatically.)
Why This Works: - The condition last_seen[char] >= left ensures we only adjust left if the duplicate is within the current window.
Problem: Given "eceba" and k=2, return 3 (substring "ece").
"eceba"
k=2
"ece"
char_count
distinct_chars
distinct_chars > k
distinct_chars <= k
from collections import defaultdict def lengthOfLongestSubstringKDistinct(s: str, k: int) -> int: char_count = defaultdict(int) left = max_len = distinct_chars = 0 for right, char in enumerate(s): if char_count[char] == 0: distinct_chars += 1 char_count[char] += 1 while distinct_chars > k: char_count[s[left]] -= 1 if char_count[s[left]] == 0: distinct_chars -= 1 left += 1 max_len = max(max_len, right - left + 1) return max_len
Time Complexity: O(n) (each character processed at most twice). Space Complexity: O(k) (storing at most k+1 characters).
k+1
Why This Works: - The frequency map tracks distinct characters, and the while loop contracts the window when the constraint is violated.
len(s) == 0
len(set(s)) == 1
max_len
right - left
right - left + 1
"a"
"Here’s your 60-second review before the interview: 1. Sliding window is the key—track left and right pointers. 2. Hash map stores the last seen index of each character. 3. Adjust left only if the duplicate is in the current window (last_seen[char] >= left). 4. Update max_len after every expansion. 5. Edge cases: Empty string, all unique, all same. 6. Optimize: Early exit if the remaining window can’t exceed max_len. You’ve got this—walk in, code it cleanly, and explain your thought process. Good luck!
Join 4M+ learners. Unlock unlimited quizzes, wrong-answer tracking, flashcards + reminders, study guides, and 1-on-1 challenges.