Asked at 12 companies
12 recent
- Zoho
- Visa
- Infosys
- Netflix
- IBM
- Cognizant
- Spotify
- Yandex
- Bloomberg
- Nvidia
+2 moreShow fewer
- Oracle
- Amazon
Community-reported · source · updated 2026-11-18. ● asked in the last 6 months.
Go step by step: try it yourself before the video and the visual. Progress saves on this device.
0 / 130 pointsSolved (Medium) 0/40Reviews 0/50Mastered 0/25Practice steps 0/15
Step 1
Know these before you start
Practise each one here, or tick what you already know
Builds on: Indexing and walking through an array · Counting with a hash map · A set for “have I seen this?”
Step 2
Try it on paper
5–10 minutes, no code yet
In the wild · API rate limits: “100 requests per minute” is a sliding window: count what falls inside the last 60 seconds, drop what slides out, and reject the extra.
ProblemProblem statementGiven a string s, find the length of the longest substring without repeating characters.ReadHide
Problem Statement
Given a string s, find the length of the longest substring without repeating characters.
Imported 2026-10-09 from NeetCode auto-sync (reviewed).
Input / Output
Input: s = "abcabcbb"
Output: 3
Explanation: "abc" is the longest valid substring.
Input: s = "pwwkew"
Output: 3
Code it here
Tab / Shift+Tab indent · ⌘/Ctrl+/ comments · ⌘/Ctrl+Enter runs · Esc leaves the editor
Step 6
Get it accepted
All test cases pass
Step 7
Review it so it sticks
Re-solve from memory after
The step that matters most: without reviews most of a solved problem fades within weeks. Three short re-solves, each just as you start to forget, are what make it yours.
Your notes
Three lines in your own words. They come back with this problem's reviews, so you can check yourself after recalling the solution.
Solve it yourself before you look
Reading the solution first feels like progress, but it makes the next similar problem — and the interview version — much harder, because you skipped the part where you figure it out. Give it an honest 20–30 minutes. Stuck? Re-read the pattern, watch the concept video, or try the brute force first.
Hidden: approach · solution code · optimal solution
Approach
Pattern Recognition
Sliding Window + Set — expand the right edge; when a duplicate appears, shrink from the left until it's gone. The set is the window's memory.
Approach
Brute Force — All Substrings, O(n²)
for each start: extend until a repeat, track max length
Optimized — Sliding Window, O(n)
l = 0, window = set()
for r in range(n):
while s[r] in window: remove s[l]; l += 1
window.add(s[r])
best = max(best, r - l + 1)
Complexity
| Approach | Time | Space | Notes |
|---|---|---|---|
| All substrings | O(n²) | O(n) | Rebuilds every window |
| Sliding window | O(n) | O(n) | Each char enters/leaves once |
Solution code
class SolutionBruteForce:
"""O(n^2) time | O(n) space — extend from each start until a repeat."""
def lengthOfLongestSubstring(self, s: str) -> int:
best = 0
for i in range(len(s)):
seen = set()
for j in range(i, len(s)):
if s[j] in seen:
break
seen.add(s[j])
best = max(best, j - i + 1)
return best
class Solution:
"""O(n) time | O(n) space — sliding window with a set."""
def lengthOfLongestSubstring(self, s: str) -> int:
window = set()
l = 0
best = 0
for r in range(len(s)):
while s[r] in window:
window.remove(s[l])
l += 1
window.add(s[r])
best = max(best, r - l + 1)
return bestOther ways to solve it
- Thought of another approach (slower, or cleverer)? Code it here: the same tests check any approach.
- Community solutions on LeetCode ↗ for many more versions, in many languages. Read them after you've solved it.
Revising this one?
- →Solve it from memory before peeking
- →Say the pattern out loud, not just the code
- →Explain it like you would to a friend
- →Try it again in a different language