<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/medium-tjownsknq" -->

---
title: Medium | daily.dev
description: A deep-dive tutorial on the Two Pointers and Sliding Window techniques, explaining how they eliminate wasteful O(n²) nested loops in favor of O(n) single-pass...
canonical: https://daily.dev/posts/medium-tjownsknq
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: Medium | daily.dev
og:description: A deep-dive tutorial on the Two Pointers and Sliding Window techniques, explaining how they eliminate wasteful O(n²) nested loops in favor of O(n) single-pass...
og:url: https://daily.dev/posts/medium-tjownsknq
og:image: https://api.daily.dev/og/posts/tJoWnsKnq.png
og:image:alt: Medium
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Medium

**[Medium](https://daily.dev/sources/medium_js)** · 45 min read · 0 upvotes · 0 comments

## Summary

A deep-dive tutorial on the Two Pointers and Sliding Window techniques, explaining how they eliminate wasteful O(n²) nested loops in favor of O(n) single-pass solutions. Covers opposite-direction and same-direction two-pointer variants, fixed-size and variable-size sliding windows, the monotonic deque pattern for sliding window maximum, and the 'at most' vs 'at least' shrinking distinction. Connects these patterns to real data engineering use cases including sort-merge joins, Spark Structured Streaming windowed aggregations, watermarking, rate limiting, CDC comparison logic, sessionization, and small-file compaction. Includes complexity analysis, comparison with hash-based approaches, common misconceptions, and practice problems.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://medium.com/@amannandan519/dsa-decoded-part-6-two-pointers-and-sliding-window-turning-wasteful-loops-into-elegant-single-ff857181e8dd>

## Questions this post answers

### How do you find the maximum sum of a contiguous subarray of size K in O(n) time instead of O(n*K)?

Use a fixed-size sliding window: compute the sum of the first K elements once, then slide forward by subtracting the element leaving the window and adding the element entering it, updating a running maximum at each step. This avoids recalculating the sum from scratch for every window, turning an O(n*K) approach into O(n).

_daily.dev surfaces algorithmic patterns like this for engineers optimizing data pipeline performance._

### How do you find the length of the longest substring without repeating characters?

Use a variable-size sliding window with a hash set tracking characters currently in the window. Expand the window by moving a right pointer forward one character at a time; whenever the new character is already in the set, shrink from the left, removing characters until the duplicate is gone. Each character is added and removed from the set at most once, so total work stays O(n) despite the nested loop appearance.

_Developers debugging string-processing logic can find pointer-based techniques like this on daily.dev._

### How do streaming engines like Spark Structured Streaming or Flink compute windowed aggregations efficiently without recalculating from scratch every time the window moves?

They apply the same sliding window pattern used for array sums: as new events enter a time window they are added to a running aggregate, and as old events fall outside the window's time boundary they are subtracted back out, mirroring the arr[i] - arr[i-k] incremental update rather than recomputing the entire window's total on every advance.

_daily.dev helps engineers building streaming pipelines connect algorithmic fundamentals to production system behavior._

## Similar posts on daily.dev

- [How I Solved LeetCode 904: Fruit Into Baskets Using Sliding Window](https://daily.dev/posts/how-i-solved-leetcode-904-fruit-into-baskets-using-sliding-window-eqawh8zrd) · Medium · 0 upvotes · 0 comments
- [Rate Limiting Algorithms: Token Bucket vs Sliding Window vs Fixed Window](https://daily.dev/posts/rate-limiting-algorithms-token-bucket-vs-sliding-window-vs-fixed-window-c6i3q4jdl) · Arcjet · 0 upvotes · 0 comments

---

Tags: [#python](https://daily.dev/tags/python), [#algorithms](https://daily.dev/tags/algorithms), [#data-structures](https://daily.dev/tags/data-structures)

[View this post on daily.dev](https://daily.dev/posts/medium-tjownsknq)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"TechArticle","headline":"Medium","url":"https://daily.dev/posts/medium-tjownsknq","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/medium-tjownsknq"},"datePublished":"2026-07-22T15:36:33.564Z","dateModified":"2026-09-13T20:41:50.544Z","description":"A deep-dive tutorial on the Two Pointers and Sliding Window techniques, explaining how they eliminate wasteful O(n²) nested loops in favor of O(n) single-pass...","image":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/9bc03078a6bafddcc0186c0bf239b4c9?_a=AQAEuop","thumbnailUrl":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/9bc03078a6bafddcc0186c0bf239b4c9?_a=AQAEuop","isAccessibleForFree":true,"articleSection":"Medium","inLanguage":"en","publisher":{"@type":"Organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180}},"author":{"@type":"Organization","name":"Medium","logo":"https://media.daily.dev/image/upload/t_logo,f_auto/v1/logos/medium","url":"https://daily.dev/sources/medium_js"},"commentCount":0,"discussionUrl":"https://daily.dev/posts/medium-tjownsknq","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":0},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":0}],"keywords":"python,algorithms,data-structures","timeRequired":"PT45M"}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"Medium","item":"https://daily.dev/sources/medium_js"},{"@type":"ListItem","position":3,"name":"Medium"}]}
{"@context":"https://schema.org","@type":"FAQPage","@id":"https://daily.dev/posts/medium-tjownsknq#faq","mainEntity":[{"@type":"Question","name":"How do you find the maximum sum of a contiguous subarray of size K in O(n) time instead of O(n*K)?","acceptedAnswer":{"@type":"Answer","text":"Use a fixed-size sliding window: compute the sum of the first K elements once, then slide forward by subtracting the element leaving the window and adding the element entering it, updating a running maximum at each step. This avoids recalculating the sum from scratch for every window, turning an O(n*K) approach into O(n). daily.dev surfaces algorithmic patterns like this for engineers optimizing data pipeline performance."}},{"@type":"Question","name":"How do you find the length of the longest substring without repeating characters?","acceptedAnswer":{"@type":"Answer","text":"Use a variable-size sliding window with a hash set tracking characters currently in the window. Expand the window by moving a right pointer forward one character at a time; whenever the new character is already in the set, shrink from the left, removing characters until the duplicate is gone. Each character is added and removed from the set at most once, so total work stays O(n) despite the nested loop appearance. Developers debugging string-processing logic can find pointer-based techniques like this on daily.dev."}},{"@type":"Question","name":"How do streaming engines like Spark Structured Streaming or Flink compute windowed aggregations efficiently without recalculating from scratch every time the window moves?","acceptedAnswer":{"@type":"Answer","text":"They apply the same sliding window pattern used for array sums: as new events enter a time window they are added to a running aggregate, and as old events fall outside the window's time boundary they are subtracted back out, mirroring the arr[i] - arr[i-k] incremental update rather than recomputing the entire window's total on every advance. daily.dev helps engineers building streaming pipelines connect algorithmic fundamentals to production system behavior."}}]}
```

