DSA: Arrays, Hashing, Two Pointers & Sliding Window
When to use
Use this skill during technical coding interviews, competitive programming, or real-time data stream processing where sub-arrays, string anagrams, target sums, or contiguous windows must be analyzed in O(N) linear time.
Core principles
- Space-Time Trade-off via Hashing: Trading O(N) memory for an instantaneous O(1) average lookup eliminates nested O(N^2) brute-force scans (e.g., Two Sum complement lookup).
- Invariant Maintenance: In two-pointer techniques (converging, fast-slow, or parallel), identify the loop invariant that guarantees pointers only advance and never oscillate.
- Dynamic vs. Fixed Sliding Windows: Fixed windows maintain a constant width
k; dynamic windows expand the right pointer to satisfy a condition and shrink the left pointer to restore validity.
- Human Anxiety & Edge Case Management: Under interview pressure, human brains jump directly to code. Resist this: test single-element lists, duplicate elements, empty strings, and negative values before writing line 1.
Step-by-step workflow
- Clarify constraints: ask about sorting status, mutation allowances, memory limits, and duplicate entries.
- Identify problem pattern: target sum pair -> hash map or sorted two-pointer; longest/shortest contiguous substring -> sliding window.
- State brute force complexity (e.g., O(N^2)) and immediately propose the optimal O(N) approach.
- Define window state variables (e.g.,
left, right, char_freq_map, matched_count).
- Execute two-pointer iteration while expanding and contracting.
- Trace through a 4-element concrete example step-by-step out loud.
Specifics & examples
# Sliding Window Template (Longest Substring Without Repeating Characters)
def length_of_longest_substring(s: str) -> int:
seen = {}
left = 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
- Two Pointers (Converging on Sorted Array):
left, right = 0, len(nums) - 1. If sum < target: left += 1 else right -= 1.
Common mistakes & pitfalls
- Off-by-one errors in sliding window length calculation (
right - left + 1 vs right - left).
- Forgetting to update the left pointer in a hash map when a duplicate is found behind the current left window boundary.
- Using nested loops inside the window expansion without realizing it could degrade to O(N^2) if left pointer resets.
Verification checklist
Related skills
- dsa-binary-search-matrix-search-variants
- dsa-stacks-queues-monotonic-stack-patterns
- coding-interview-communication-thinking-out-loud
- dsa-time-space-complexity-big-o-analysis
1---2name: dsa-arrays-hashing-two-pointers-sliding-window3description: Use this skill when tackling array and string algorithmic challenges using hash maps, two pointers, and dynamic sliding windows.4metadata:5 source: https://skillmd.com/skills/nextboxis/dsa-arrays-hashing-two-pointers-sliding-window6---78# DSA: Arrays, Hashing, Two Pointers & Sliding Window910## When to use11Use this skill during technical coding interviews, competitive programming, or real-time data stream processing where sub-arrays, string anagrams, target sums, or contiguous windows must be analyzed in O(N) linear time.1213## Core principles14- **Space-Time Trade-off via Hashing**: Trading O(N) memory for an instantaneous O(1) average lookup eliminates nested O(N^2) brute-force scans (e.g., Two Sum complement lookup).15- **Invariant Maintenance**: In two-pointer techniques (converging, fast-slow, or parallel), identify the loop invariant that guarantees pointers only advance and never oscillate.16- **Dynamic vs. Fixed Sliding Windows**: Fixed windows maintain a constant width `k`; dynamic windows expand the right pointer to satisfy a condition and shrink the left pointer to restore validity.17- **Human Anxiety & Edge Case Management**: Under interview pressure, human brains jump directly to code. Resist this: test single-element lists, duplicate elements, empty strings, and negative values before writing line 1.1819## Step-by-step workflow201. Clarify constraints: ask about sorting status, mutation allowances, memory limits, and duplicate entries.212. Identify problem pattern: target sum pair -> hash map or sorted two-pointer; longest/shortest contiguous substring -> sliding window.223. State brute force complexity (e.g., O(N^2)) and immediately propose the optimal O(N) approach.234. Define window state variables (e.g., `left`, `right`, `char_freq_map`, `matched_count`).245. Execute two-pointer iteration while expanding and contracting.256. Trace through a 4-element concrete example step-by-step out loud.2627## Specifics & examples28```python29# Sliding Window Template (Longest Substring Without Repeating Characters)30def length_of_longest_substring(s: str) -> int:31 seen = {}32 left = max_len = 033 for right, char in enumerate(s):34 if char in seen and seen[char] >= left:35 left = seen[char] + 136 seen[char] = right37 max_len = max(max_len, right - left + 1)38 return max_len39```40- **Two Pointers (Converging on Sorted Array)**: `left, right = 0, len(nums) - 1`. If `sum < target: left += 1` else `right -= 1`.4142## Common mistakes & pitfalls43- Off-by-one errors in sliding window length calculation (`right - left + 1` vs `right - left`).44- Forgetting to update the left pointer in a hash map when a duplicate is found behind the current left window boundary.45- Using nested loops inside the window expansion without realizing it could degrade to O(N^2) if left pointer resets.4647## Verification checklist48- [ ] Time complexity is strictly O(N) and space complexity is explicitly stated (O(1) or O(K)).49- [ ] Handled empty input, single character string, and strings with all identical characters.50- [ ] Two-pointer boundaries prevent index-out-of-bounds exceptions.5152## Related skills53- dsa-binary-search-matrix-search-variants54- dsa-stacks-queues-monotonic-stack-patterns55- coding-interview-communication-thinking-out-loud56- dsa-time-space-complexity-big-o-analysis