CSAlgorithms & ComplexityDiagramBeginner
Sliding Window
KO: 슬라이딩 윈도우·EN: Sliding Window·JA: スライディングウィンドウ
A technique that slides a contiguous range across an array or stream, updating state incrementally in O(1) per shift for O(N) total time
Table of Contents
Structure Diagram
Sliding Window Incremental Range Shift
Step 1: [0..3] sum=142 steps
Step 2: [1..4] -arr[0] +arr[4]3 steps
STEP 1Active Window (4)(40 idx)
Quick Answer
What is Sliding Window?
A technique that slides a contiguous range across an array or stream, updating state incrementally in O(1) per shift for O(N) total time. It is a core concept in Algorithms & Complexity within CS.
Core Principles
Sliding Window maintains a contiguous subarray or time window and updates its aggregate incrementally—adding the entering element and removing the leaving element—reducing O(N·K) recomputation to O(N).
- Fixed-Size Window: Constant width
Kshifting one step at a time (moving averages, rolling sums). - Variable-Size Window: Expand
rightto explore and shrinkleftwhenever a constraint is violated (longest substring without repeating characters).
Practical Takeaways
- Watch out for negative numbers: Variable-size sum windows rely on monotonicity (shrinking decreases sum). With negative values, switch to Prefix Sum + Hash Map.
- Precision API Rate Limiting: Unlike fixed-window counters that suffer boundary traffic spikes, Sliding Window Counters enforce smooth per-minute quotas.
- Network Pipelining: TCP uses a sliding window to keep multiple unacknowledged packets in flight up to the receiver's buffer capacity.
Notable Examples
- TCP Flow Control: Maximizing link utilization with in-flight packet windows.
- API Rate Limiters: Redis-backed sliding windows throttling abusive clients.
- Stream Analytics: Rolling 5-minute error rates and latency averages in Kafka Streams or Flink.