Sliding Window Technique Problem

viaGlassdoor

Problem: A sliding-window problem, e.g., find the maximum/minimum sum (or count satisfying a condition) of a contiguous subarray of size k, or the smallest window satisfying a constraint.

Constraints: Array length up to 10^5; window size k given or variable.

Example: Input: [2,1,5,1,3,2], k=3 -> max sum subarray of size 3 is [5,1,3]=9.

Approach: Maintain a running window sum/state as you slide one element at a time: add the incoming element, remove the outgoing element, and update the answer - avoiding recomputation from scratch for each window. Runs in O(n) time versus the O(n*k) brute force.

Add a follow-up question they asked
No follow-ups yet. Be the first to add one.
asked …
LeaderboardSalary
Language
Account