Lesson overview · Free interview practice

Sliding Window

Optimize window-based computations for arrays and strings.

Topics in the full lesson
  • Concept and Use Cases
  • When to Use
  • Time and Space Complexity
  • Fixed Size Sliding Window
  • Variable Size Sliding Window

Try the free interview questions

Work through a question before revealing its explanation. These questions and answers are free; Premium adds the full lesson walkthrough, examples and implementation detail.

Question

Given two strings s1 and s2, return true if s2 contains any permutation of s1 as a contiguous substring; otherwise return false. (Equivalently: does a sliding window of size s1.length in s2 have the same character frequencies as s1?)

Take a moment to think about this before revealing the answer

Explain your reasoning or try an implementation before comparing answers.

Question

Given an array of positive integers nums and a positive integer target, return the minimal length of a contiguous subarray whose sum is greater than or equal to target. If no such subarray exists, return 0.

Take a moment to think about this before revealing the answer

Explain your reasoning or try an implementation before comparing answers.

CodingMedium

Fruit Into Baskets

Question

You are visiting a fruit farm where the trees are in a row, represented by an array fruits (each value is a fruit type). You have two baskets and each basket can only hold one type of fruit (no count limit). You start at any tree, must move strictly rightward, and must pick one fruit from every tree until you cannot continue. Return the maximum number of fruits you can pick — equivalently, the longest contiguous subarray containing at most 2 distinct values.

Take a moment to think about this before revealing the answer

Explain your reasoning or try an implementation before comparing answers.

Question

Given a binary array nums, you must delete exactly one element. Return the length of the longest subarray containing only 1s in the resulting array.

Take a moment to think about this before revealing the answer

Explain your reasoning or try an implementation before comparing answers.

Explore the full Sliding Window material

Premium includes the complete lessons and implementation references. Free practice questions remain available without a subscription.

All course tracks & premium content
From basics to advanced masterclasses
Built for JS/TS developers like you
Real-world tips & common pitfalls
Upgrade to Premium