Sliding windows, sweeps and binary search
O(n) windows, sort-then-sweep for intervals, and bisect for as-of lookups.
Foundations · Python for data engineers · Gold · Algorithms interviewers ask, lesson 2 of 3
On this page
Read first: Counter, defaultdict and deque (skip if you know it)
In plain words first
Sliding window
A train window: as the train moves, one view enters on one side and one leaves on the other. You never look at the whole landscape again.
Two pointers
Two people walking towards each other along a sorted row, each stepping in depending on what they see.
Binary search
Finding a word in a dictionary: open the middle, decide which half, repeat. A million words in about 20 steps.
Unlock the full lesson
You have read the free part. The rest of this advanced lesson:
- Three patterns behind many questions
- Sliding windows
- Sort, then sweep
- Binary search with bisect
- Common mistakes
- Key takeaways
- Interview prep with 3 follow-up questions
- Check yourself: 3 questions
Unlock it with the Complete pass (every advanced problem and lesson) or on its own.
Practise this
Problems that use this, graded on hidden tests
- Valid BracketsDSA for data engineers Easy
- Longest Substring Without RepeatsDSA for data engineers Medium
- Latest Event at or Before a TimeDSA for data engineers Medium
Keep going
Up next · Algorithms interviewers ask · lesson 3 of 3 · 7 min readGraphs and topological sort
Dependency graphs, Kahn's algorithm, cycle detection and how schedulers use them.
Related lessons
Previous: Heaps, top-k and k-way merges
Primary sources: bisect · collections.deque