Syllabus Lesson 58 of 141 · Speed without the cache
Speed without the cache

Here the cache is right

Why it matters After four problems where the cache lost, it is tempting to never cache. That is a new default, not judgment.

A nightly job converts the day's card transactions to pounds. There are about 5,000 transactions in around 30 currencies. Each call to the rate service is billed, and rates are fixed for the batch: the service publishes one rate per currency per night. The next night, rates are new.

Check the three conditions for a cache: do questions repeat, do answers stay fresh, is there room?

Your turn

Write convert_all(transactions, fetch_rate). transactions is a list of (currency, amount) pairs; fetch_rate(currency) returns that currency's rate to pounds. Return a list of round(amount * rate, 2), one per transaction, in order. Rules: call fetch_rate at most once per currency in a batch; rates change between batches, so do not carry rates from one call of convert_all to the next; your whole code may have at most 70 syntax nodes (code_size).

Spotted a problem in this lesson? Report it · Ask about this lesson

This exercise opens soon

Finish the lesson before it and this one unlocks. The whole course is free, no account needed, and you can read this lesson right now.