Module 4 · 4 lessons · about 7 hours 5 minutes
Two pointers and sliding windows
Pointers that close in from both ends, read and write pointers that edit a list in place, and windows that slide, grow and shrink over a list or string.
- Lessons
- 0/4
- Exercises
- 0/30
- Code
- 0/4
- Problems
- 0/15
- Checkpoint
- Not taken
Already comfortable with this? Take the checkpoint cold. If you pass, move on and come back only for what you missed.
- 1Two pointers from both endsPut one index at each end of a list or string and walk them toward each other. Every step proves that one item cannot be part of a better answer, so problems that seem to need every pair take one pass.2 h
- 2Read and write pointersTwo indexes walk one list in the same direction. `read` looks at every item, `write` marks where the next kept item goes, and everything before `write` is already the finished answer. Filled from the back, the same idea merges two sorted lists in place.1 h 35 min
- 3Fixed-size windowsA window is a block of k items in a row. Instead of adding up every window from scratch, slide it one step at a time, adding the item that enters on the right and subtracting the item that leaves on the left. O(n·k) work becomes O(n).1 h 10 min
- 4Windows that grow and shrinkWhen the question asks for the longest or shortest block that obeys a rule, one window answers it in one pass. The right edge moves every step, and the left edge catches up only when the rule forces it.2 h 20 min
- Checkpoint12 questions. Pass with 80% to finish the module.
Problems for this module
The lessons teach some of these step by step (each says which). The rest are practice: try each in its target time, and solved problems come back for spaced re-solves.
- easyValid palindrome15 min
- mediumTwo sum in a sorted list25 min
- mediumContainer with the most water25 min
- mediumThree sum25 min
- easySquares of a sorted list15 min
- easyRemove duplicates from a sorted list15 min
- easyMove zeroes to the end15 min
- easyMerge two sorted lists in place15 min
- mediumSort three colors25 min
- easyLargest average window15 min
- mediumA rearrangement inside a string25 min
- mediumLongest stretch without a repeat25 min
- mediumShortest block that reaches a target25 min
- mediumLongest run after a few changes25 min
- hardSmallest window that covers a set of letters40 min