Welcome to Day 2 of your coding interview journey.
Yesterday we covered HashMaps, and today weβll combine that knowledge with a powerful technique used in many top interview problems:
π Sliding Window
This pattern helps you turn slow O(nΒ²) solutions into efficient O(n) ones.
π§ What is the Sliding Window Technique?
The Sliding Window is used to process subarrays or substrings efficiently by maintaining a βwindowβ that moves through the data.
Instead of recalculating everything repeatedly, you:
- Expand the window β‘οΈ
- Shrink the window β¬ οΈ
- Keep track of useful data (often using a HashMap or Set)
π When Should You Use Sliding Window?
Think about this pattern when you see:
- Substrings / subarrays
- βLongestβ, βShortestβ, βMaximumβ, βMinimumβ
- Contiguous sequences
- Conditions involving uniqueness or counts
π‘ Interview Pattern β Longest Substring Without Repeating Characters
This is a must-know problem. It appears in interviews at companies like Google, Amazon, and Meta.
π§© Problem
Given a string s, find the length of the longest substring without repeating characters.
β Brute Force Approach
- Generate all substrings
- Check each one
π Time Complexity: O(nΒ²) (too slow for interviews)
β Optimal Approach β Sliding Window + Set
We use:
- A set to track characters in the window
- Two pointers (
leftandright)
π οΈ Solution in Python
def longest_substring(s):
char_set = set()
left = 0
max_length = 0
for right in range(len(s)):
# If duplicate, shrink window
while s[right] in char_set:
char_set.remove(s[left])
left += 1
char_set.add(s[right])
max_length = max(max_length, right - left + 1)
return max_length
β Example
s = "abcabcbb"
print(longest_substring(s))
Output:
3 # "abc"
π― Step-by-Step Intuition
For "abcabcbb":
- Start expanding:
"a" β "ab" β "abc"β - Hit duplicate
"a"β - Move left pointer until valid again
- Continue…
π The window always contains unique characters
β‘ Complexity Analysis
- Time Complexity:
O(n)
Each character is visited at most twice - Space Complexity:
O(min(n, charset))
π₯ Optimized Version (Using HashMap)
We can skip unnecessary steps by jumping the left pointer:
def longest_substring_optimized(s):
char_map = {}
left = 0
max_length = 0
for right, char in enumerate(s):
if char in char_map and char_map[char] >= left:
left = char_map[char] + 1
char_map[char] = right
max_length = max(max_length, right - left + 1)
return max_length
π This version is cleaner and often preferred in interviews.
β οΈ Common Mistakes
- β Forgetting to shrink the window properly
- β Not updating
leftcorrectly - β Using brute force instead of optimizing
- β Confusing substring vs subsequence
π§ͺ Practice Problems
To master Sliding Window, try:
- Longest repeating character replacement
- Minimum window substring
- Permutation in string
- Maximum sum subarray of size K
- Find all anagrams in a string
π― Key Takeaways
- Sliding Window reduces complexity from O(nΒ²) β O(n)
- Works best with:
- Contiguous data
- Dynamic conditions
- Often combined with:
- HashMaps
- Sets
π Your Challenge (Day 2)
Solve this:
Given a string, find the length of the longest substring with at most 2 distinct characters.
π Final Thoughts
Sliding Window is a game-changer for coding interviews.
If you master this pattern, youβll unlock a huge category of problems that many candidates struggle with.
Tomorrow, weβll continue leveling up. πͺ