Geek Culture
Published in

Geek Culture

You have 2 free member-only stories left this month.

Coding Interview Made Easy: Understanding the Sliding Window Pattern

Coding Patterns
Array: [1, 3, 2, 6, -1, 4, 1, 8, 2], K=5
  1. For the first 5 numbers (subarray from index 0–4), the average is: (1+3+2+6–1)/5 => 2.2
  2. The average of next 5 numbers (subarray from index 1–5) is: (3+2+6–1+4)/5 => 2.8
  3. For the next 5 numbers (subarray from index 2–6), the average is: (2+6–1+4+1)/5 => 2.4
Output: [2.2, 2.8, 2.4, 3.6, 2.8]

Brute-force algorithm

Brute-force algorithm

Sliding Window to the rescue

Sliding Window algorithm
  1. Maximum Sum Subarray of Size K (easy)
  2. Smallest Subarray with a given sum (easy)
  3. Fruits into Baskets (medium)
  4. Longest Substring with K Distinct Characters (medium)
  5. Longest Substring with Distinct Characters (hard)
  6. Permutation in a String (hard)
  7. String Anagrams (hard)
  8. Words Concatenation (hard)

Thanks for reading

Sign up for Geek Culture Hits

By Geek Culture

Subscribe to receive top 10 most read stories of Geek Culture — delivered straight into your inbox, once a week. Take a look.

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Medium sent you an email at to complete your subscription.

A new tech publication by Start it up (https://medium.com/swlh).

Share your ideas with millions of readers.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store