Home/Data Structures & Algorithms/Two Pointers & Sliding Window

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Multi-pointer convergence, dynamic sliding windows, stream boundary tracking, and monotonic deque window optimization.
Minimum Window Substring: Why an integer frequency array beats HashMap in low-latency parsers
Minimum Window Substring is the crown jewel of sliding window problems. The difference between a junior solution and a staff engineer solution comes down to how window validation is tracked. 1. The Trap: Comparing Two Hash Maps In naive implementations, developers maintain two hash maps: target_counRead more
Minimum Window Substring is the crown jewel of sliding window problems. The difference between a junior solution and a staff engineer solution comes down to how window validation is tracked.
1. The Trap: Comparing Two Hash Maps
In naive implementations, developers maintain two hash maps:
target_countsandwindow_counts. Whenever the window slides, they loop through the keys oftarget_countsto see if the window is valid. That turns an $O(N)$ algorithm into $O(N cdot |Sigma|)$ with heavy hash table overhead!2. The Staff Engineer Pattern: Single Vector + Deficit Counter
We can optimize this down to bare metal with two simple tricks:
required): We setrequired = len(t). When expanding the window with pointerr, ifcounts[s[r]] > 0, that character was actively needed, so we decrementrequired--. Whenrequired == 0, the window is 100% valid! We don’t have to check any other variables!Clean Python 3.12 Implementation
Complexity Breakdown
- Time Complexity:
- Space Complexity:
See lessO(|s| + |t|). Therightpointer advances|s|times. Theleftpointer advances at most|s|times. Total pointer advances =2|s|.O(1)auxiliary space. Exactly 128 integers on the stack.Why does the Two-Pointer approach beat Monotonic Stack for Trapping Rainwater in production?
I see this question come up all the time in engineering interviews and production optimizations. The short answer is: memory allocations and CPU cache locality. On paper, both the Monotonic Stack and Two Pointers are O(N) time. But in reality: Monotonic Stack: Pushes and pops indices into a dynamicRead more
I see this question come up all the time in engineering interviews and production optimizations. The short answer is: memory allocations and CPU cache locality.
On paper, both the Monotonic Stack and Two Pointers are
O(N)time. But in reality:std::stackin C++ or a dynamic slice in Python/Go). That means repeated memory allocations, pointer indirection, and cache misses every time the stack resizes or wanders through heap memory.left,right,left_max,right_max). These variables stay entirely inside CPU registers. There is zero heap allocation, zero pointer chasing, and the CPU prefetcher streams the array from both ends sequentially at full hardware bus speed.The Plain English Intuition
Think about standing at the edge of a swimming pool. The amount of water that can sit on top of any single column
iis strictly decided by one thing: the shorter of the two tallest walls on its left and right.Mathematically:
water[i] = max(0, min(max_left, max_right) - height[i]).Here is the genius of two pointers: you place one pointer at the start (
left) and one at the end (right). At every step:height[left] <= height[right], you know for certain that whatever tall wall exists on the far right is at least as tall asheight[left]. So the bottleneck for the left side is only determined byleft_max. You can safely calculate water atleftand moveleft++.height[right] < height[left], the exact opposite holds true. The bottleneck forrightis determined purely byright_max. You calculate water atrightand moveright--.You never have to look back, and you never have to store past heights in a stack!
Production-Ready Python 3.12 Implementation
Clean C++20 Version (Zero Allocations)
Complexity & Production Pitfalls
- Time Complexity:
- Space Complexity:
- 32-bit Integer Overflow: If you have an array of
See lessO(N). Every element is visited exactly once. No nested loops.O(1). No auxiliary memory allocated.100,000elements, each with height100,000, the total water can reach10^10. A standard 32-bit signed integer will overflow and return a negative number! Always use a 64-bit integer (int64_tin C++ orlongin Java) for the accumulator.