TGViewer
Coding Interview Preparation Coding Interview Preparation @coding_interview_preparation · 5.9K subscribers
Post #1380 353
🎯 CODING CHALLENGE #5 - Longest Substring Without Repeating Characters
Difficulty: Medium | Asked at: Amazon, Meta, Bloomberg


Input: "abcabcbb"
Output: 3 ("abc")

Input: "bbbbb"
Output: 1 ("b")


💡 Hint: This screams sliding window. Keep expanding a window to the right, and when you hit a repeat, shrink from the left until the repeat is gone.

Solution:
python
def length_of_longest_substring(s):
seen = {}
left = 0
max_len = 0

for right, char in enumerate(s):
if char in seen and seen[char] >= left:
left = seen[char] + 1
seen[char] = right
max_len = max(max_len, right - left + 1)

return max_len


Complexity: O(n) time - each character is visited by right once, and left only ever moves forward. O(min(n, alphabet size)) space for the hash map.

Common mistake: Resetting left to seen[char] + 1 even when the previous occurrence of char is OUTSIDE the current window (i.e., seen[char] < left). Without the seen[char] >= left check, you can accidentally move left backwards, which breaks the algorithm.

Sliding window is one of the highest-ROI patterns to master - it solves a huge chunk of "substring" and "subarray" problems. Comfortable with it, or still building intuition? 👇
More from @coding_interview_preparation
  1. Oct 8, 2026If you're prepping for system design interviews, this repo is gold It contains a curated,…
  2. Oct 6, 2026document post
  3. Oct 4, 2026💼 Why Your Resume Gets Rejected Before a Human Reads It You may have good skills and proj…
  4. Oct 2, 2026🧠 Coding Myths You Should Stop Believing There's a lot of advice online about learning to…
  5. Oct 1, 2026Most Asked Topics in AI Engineer Interviews Based on 2026 candidate reports
  6. Sep 30, 2026💼 What Companies Actually Look For in a Fresher Think companies only care about your CGPA…
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook →Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 →