The Longest Substring Without Repeating Characters is a foundational computer science problem. Given a string s, you must find the length of the longest contiguous sequence of characters where no character appears more than once. For example, in the string "abcabcbb", the longest substring without repeating characters is "abc", which has a length of 3.
This problem is asked in nearly every tech company's interview loops (from startups to FAANG) because it is the ultimate test of the Sliding Window technique. It evaluates a candidate's ability to optimize a naive or brute-force check down to a highly efficient linear scan. It effectively measures basic data structure knowledge, pointer manipulation, and edge-case handling.
This is the textbook example of the Sliding Window and Hash Set / Hash Map pattern. You maintain a left and right pointer to represent the current window. As right iterates through the string, you add characters to a Hash Set. If the character at right is already in the set (a duplicate is found), you must shrink the window by moving the left pointer and removing characters from the set until the duplicate is eliminated.
String: "pwwkew"
right=0 ('p'): Set {'p'}. Max = 1.right=1 ('w'): Set {'p', 'w'}. Max = 2.right=2 ('w'): Duplicate found! The set already has 'w'. We must move left.
left=0 ('p'): Remove 'p'. Set {'w'}. 'w' is still a duplicate!left=1 ('w'): Remove 'w'. Set {}. Duplicate gone!{'w'}. Window is "w".right=3 ('k'): Set {'w', 'k'}. Max = 2.right=4 ('e'): Set {'w', 'k', 'e'}. Max = 3. ("wke")right=5 ('w'): Duplicate 'w'. Move left past the first 'w'. Window becomes "kew". Max remains 3.A very common mistake is clearing the entire Hash Set when a duplicate is found and resetting the left pointer to right. This discards valuable non-repeating characters! For example, in "dvdf", hitting the second 'd' shouldn't wipe out the 'v'. You must incrementally slide the left pointer, removing only the characters that fall outside the new window, until the specific duplicate is ejected.
To master this interview pattern, learn the Hash Map optimization. Instead of using a Set and moving left step-by-step, use a Hash Map to store the index of each character. When you hit a duplicate, you can instantly jump the left pointer to map.get(char) + 1 (as long as that jump is forward!). This turns a algorithm into a strict one-pass solution.