Lesson 3 of 30
Sometimes you can make code faster by having it remember answers it already worked out. That trade — memory for speed — is one of the most useful ideas in this whole track.
10 min read
Every option explains itself — why the right one is right, and why each wrong one is wrong. Miss one and it comes back later on a spaced-repetition schedule.
This topic is about judgment and practical tradeoffs more than a single right answer — a mock interview is the better way to test yourself on it.
Be interviewed on this →Hints without spoilers, and a plain explanation of why a wrong answer was wrong — answered only from this lesson. Free account, no card.
Sign up freeWhat interviewers actually ask about this, and what a strong answer has to cover.
A strong answer covers
Likely follow-ups
Imagine you're doing the same math problem over and over for different people throughout the day — say, converting Celsius to Fahrenheit. You could redo the multiplication every single time someone asks. Or, the first time someone asks for 20°C, you could jot the answer on a sticky note. Next time anyone asks for 20°C, you just read the sticky note instead of doing the math again.
That sticky note costs you something: a little bit of space on your desk. But it saves you something bigger: time, every time the same question comes up again. That's the whole trade. You're not getting something for nothing — you're spending memory (the sticky notes) to buy speed (skipping repeated work).
In programming, this has a name — memoization — but don't worry about the word yet. Worry about the idea: if your code is about to do the same expensive work twice, consider having it remember the answer the first time, so the second time is almost free.
This is called the time–space trade-off, and it's not just one trick — it's a lens. Any time you're solving a problem, you can ask: "Am I trading time for space here, or space for time?" Sometimes the answer is that saving time isn't worth the extra memory. Sometimes it obviously is. Either way, noticing the trade-off is the skill — not always choosing speed.
// Without memory: recalculates the slow thing every single call.
function slowSquare(n) {
// pretend this represents some genuinely expensive work
for (let i = 0; i < 100000000; i++) {} // wastes time on purpose
return n * n;
}
// With memory: a "sticky note" (the cache object) remembers past answers.
const cache = {};
function memoizedSquare(n) {
if (n in cache) {
return cache[n]; // already worked this one out — just read the note
}
const result = slowSquare(n); // do the expensive work once
cache[n] = result; // write the sticky note for next time
return result;
}
console.log(memoizedSquare(5)); // slow the first time
console.log(memoizedSquare(5)); // instant the second time — read from cache
This same "remember instead of redo" idea is what powers the JavaScript track's discussion of caching and, later in this track, the arrays lesson's box-of-numbers picture — memory is the boxes, and this lesson is about deciding when it's worth using more of them.