Sliding Windows Explained: Sunglasses Analogy with Ruby Code

Published: (January 19, 2026 at 10:26 PM EST)
3 min read
Source: Dev.to

Source: Dev.to

The Sunglasses Analogy 😎🌄

Imagine you are standing on a hill overlooking a long landscape. The landscape represents your array or string, and you have a special pair of sunglasses that only let you see a fixed portion at a time — this is your sliding window.

  • Entering view: the new element that comes in on the right side of the window.
  • Exiting view: the element that leaves on the left side of the window.
  • State tracking: you keep track of something interesting inside the window (e.g., sum, max, number of unique elements).

Instead of recalculating everything for each new position, you update incrementally:

  1. Remove what leaves your view.
  2. Add what enters your view.
  3. Update your result.

Without sliding windows

For each window of size k you would recompute everything from scratch → slow.

With sliding windows

You update the state as the window moves → fast and efficient.

Example 1: Maximum Sum of a Fixed‑Size Window

# Landscape: array of trees
arr = [1, 2, 3, 4, 5]
k   = 3

# Initial window sum
window_sum = arr[0...k].sum
max_sum    = window_sum

# Slide the window
(1..arr.length - k).each do |i|
  window_sum = window_sum - arr[i - 1] + arr[i + k - 1]
  max_sum    = [max_sum, window_sum].max
end

puts "Maximum sum of a window of size #{k}: #{max_sum}"

Explanation

  • arr[i - 1] leaves the window.
  • arr[i + k - 1] enters the window.
  • The window_sum is updated incrementally, avoiding a full recomputation.

Example 2: Longest Substring with ≤ k Distinct Characters

s = "abcba"
k = 2

count   = Hash.new(0)  # frequency of characters in the current window
left    = 0
max_len = 0

(0...s.length).each do |right|
  count[s[right]] += 1

  # Shrink window from the left while we have more than k distinct chars
  while count.size > k
    count[s[left]] -= 1
    count.delete(s[left]) if count[s[left]] == 0
    left += 1
  end

  max_len = [max_len, right - left + 1].max
end

puts "Length of longest substring with ≤ #{k} distinct characters: #{max_len}"

Explanation

  • Expand the window to the right by adding s[right].
  • If the number of distinct characters exceeds k, shrink from the left until the constraint is satisfied.
  • count maintains the state (character frequencies) inside the window.

Key Takeaways

  • Two pointers (left and right) naturally represent the sliding window.
  • Focus on incremental updates: add the new element, remove the old one, and adjust the tracked state.
  • Visualizing the process with the sunglasses metaphor helps turn a seemingly tricky technique into an intuitive one.

Tip: Start with small, handwritten examples using the sunglasses metaphor, then translate the steps into code. This reinforces the concept and makes solving sliding‑window problems much easier. 😎

Back to Blog

Related posts

Read more »

Problem 10: Duplicate Removal

Problem Description We need a function that removes duplicates from a list while preserving the original order of elements. Example remove_duplicates1, 2, 2, 3...