Interview concept

What is the sliding window technique?

The sliding window technique keeps a moving range over an array or string and updates a running result as the window grows or shrinks, turning many O(n squared) brute-force scans into O(n) solutions.

Interview concept

How it works

Fixed vs dynamic windows

A fixed window keeps a constant size; a dynamic window expands and contracts based on a condition such as a sum or character count.

When to use it

Reach for it on contiguous-subarray or substring problems asking for a maximum, minimum, or count under a constraint.

Common pitfalls

Watch off-by-one bounds, when to shrink the window, and correctly updating state as elements leave.

Interview concept

Responsible use

Use any live AI assistant only where interview, workplace, school, and platform rules allow it. Do not use generated answers to misrepresent your skills, experience, or authorship.

FAQ

Common questions.

When should I use a sliding window?

Use it for contiguous subarray or substring problems where you can update a result incrementally as the range moves.

What is the time complexity?

Most sliding window solutions run in O(n) because each element enters and leaves the window at most once.