Module 4 · Arrays & Dynamic Arrays
Practice
Practice~1 min
How to practice this module
Arrays drills reward in-place discipline: write pointers, partitions, and single-pass bookkeeping. Work the five problems in order — each one reuses an idea from the previous concept lessons, then twists it.
Suggested loop for each problem: attempt the sandbox cold → read the explanation only after a real try → re-solve from memory the next day. You are done with Arrays Practice when all five show Solved in the hub.
Problems
Arrays — work them in order; difficulty ascends.
0/5
solved
- 1Remove Duplicates from Sorted ArrayEasyTwo pointers / write pointerWatch for: Off-by-one on the write index; do not allocate a second array
- 2Move ZeroesEasyPartition pointersWatch for: Stability of non-zero order; zeroes must survive at the end
- 3Best Time to Buy & Sell StockEasySingle-pass running minimumWatch for: Sell day must be after buy day; all-falling prices → 0
- 4Rotate ArrayMediumReverse cycles / triple reverseWatch for: k can exceed n — normalise before indexing; O(1) extra space
- 5Product of Array Except SelfMediumPrefix / suffix productsWatch for: No division; zeroes break the naive product/divide trick
Cheatsheet
Arrays — In-place discipline — write pointers, partitions, one pass.
Smell → pattern
- Sorted array, remove / unique in placeWrite pointer
- Reorder around a predicate (zero / pivot)Partition
- Rotate or reverse cycles, O(1) spaceTriple reverse
- Best of something over a scanRunning extreme
- Answer needs left×right without divisionPrefix / suffix
Patterns
Write pointer
CoreSmell: Compact valid elements to the front
Read with one index, write with another. Everything left of write is the answer region; never allocate a second array.
Partition pointers
SafeSmell: Stable reorder by a boolean test
Grow a ‘kept’ region from the left. Swap (or assign) when the predicate holds; watch relative order if the problem demands stability.
Triple reverse
ReachSmell: Rotate by k with O(1) extra memory
Reverse whole → reverse prefix → reverse suffix. Normalise k mod n before indexing; empty and n=1 are free no-ops.
Running minimum / maximum
CoreSmell: Optimal pair from a left-to-right scan
Track the best so far while scanning once. The sell/buy (or similar) day ordering is an invariant — encode it in the scan direction.
Prefix × suffix products
CarefulSmell: Each index needs product of all others
Build left products, then fold right products on a second pass. Division tricks die on zeroes — don’t use them.
Complexity targets
Write / partition pass
- Time
- O(n)
- Space
- O(1)
- Note
- Target for in-place drills
Triple reverse rotate
- Time
- O(n)
- Space
- O(1)
- Note
- Three linear reverses
Prefix / suffix products
- Time
- O(n)
- Space
- O(1)*
- Note
- *output array doesn’t count
| Move | Time | Space | Note |
|---|---|---|---|
| Write / partition pass | O(n) | O(1) | Target for in-place drills |
| Triple reverse rotate | O(n) | O(1) | Three linear reverses |
| Prefix / suffix products | O(n) | O(1)* | *output array doesn’t count |
Traps
Off-by-one on the write index
Decide whether write points at the next free slot or the last written one — and stick to it. Mixing the two is the usual silent corruption.
k can exceed n
Any rotate or index arithmetic that ignores k %= n will read off the end. Normalise before you touch the array.