Module 01 · DSA · The toolbox · about 18 min to read
Continue where you left off ×
Arrays & Strings
Arrays and Strings · Open board
One row of same-size boxes, then two ways of walking it that turn a nested loop into a single pass. Builds on Complexity & Big-O and The Memory Hierarchy (cache, locality, the memory wall) .
Before you start. You need Complexity & Big-O ‘s two words: O(1) means the cost never changes as the pile grows, O(n) means it climbs in step with it. Every trick below is about touching as few boxes as possible.
The idea in 30 seconds
An array is one row of same-size boxes. Box i ’s address is one multiply and one add, so lookup costs the same at any size.
Text is a row of characters too. Many languages rebuild the whole row on every edit, so gluing pieces together in a loop quietly costs far more than it looks.
Two fingers walking toward each other from opposite ends replace checking every pair with one pass.
Running totals turn “add up this range” into one subtraction, after a single walk down the row.
Read the story
Lesson 3 of 96 in DSA · Free with an account
Keep reading. It's free.
A free account opens lessons 2 and 3 of every track, 33 more lessons, and keeps your progress and review cards on every device.
No card, and chalk sends no email. Google sign-in only.
Start free with Google
In this lesson A street where every house has a number 1945: von Neumann’s row of numbered boxes Adding to the end is free. Adding in front costs everyone. 1972: Ritchie’s one-byte decision Two fingers, walking toward the middle A window that grows and shrinks Asking about a range, over and over The array and the dynamic array Strings are arrays of characters Two-pointer, measured Sliding window & prefix sums, measured Terms it explains: array, immutable, two pointers, sliding window, prefix sum, O(1) random access, dynamic array, capacity, amortized O(1), immutability, O(n²), KMP, Floyd's tortoise & hare, fixed window, variable window, Prefix sums, capacity vs length, fixed vs variable window, Floyd's cycle detection, in-place, string, StringBuilder / join, two-pointer.
Review DSA
Mark as read Saved in this browser, and to your account when you sign in.