pattern / EASY
Use two references to traverse a data structure
complexity
time
O(n)
space
O(1)
02' — visual
interactive
Maximum sum subarray
03'
mental
model.
Two pointers move through the data structure based on a condition. Often one starts at the beginning and one at the end, moving toward each other.
Two Pointers is a technique where two pointers iterate through the data structure in tandem, often used to find pairs or compare elements efficiently.
when to use ✓
when not ✗
04'
Sorted array or linked list
Need to find pairs/triplets with a target sum
Need to compare elements from both ends
In-place operations on array
05'
1# Beginner: Fixed-size sliding window2def max_sum_k(nums, k):3 n = len(nums)4 if n < k:5 return 06 window_sum = sum(nums[:k])7 max_sum = window_sum8 for i in range(k, n):9 window_sum += nums[i] - nums[i - k]10 max_sum = max(max_sum, window_sum)11 return max_sum
common mistakes
interview tips
06'
5 curated problems from LeetCode
07'