Final challenge: the slow lookup mystery
After this: Find and fix the problems in a slow, broken cache, in five steps.
A teammate's service keeps a cache in front of a slow database. As you saw in Hash maps in practice, a cache is a hash map that keeps answers so that they do not have to be fetched again. But this cache is somehow slower than the database, and you have been asked to find out why.
Everything you need is in the earlier lessons. The five steps below follow the investigation in order, and each step is scored on its own.
You can try as many times as you like, and hints work as before. There is no pass mark. The results page shows how you did on each of the tutorial's outcomes.
Step 1 of 5Trace it
Trace it
An 8-slot table starts empty. Run put(cat, 1), put(dog, 2), put(act, 3), put(cat, 4), delete(dog), put(tac, 5). What does slot 0 hold afterwards?
Diagnose it
The service's cache uses this hash function. The keys are user names, mostly 5 to 7 letters long. What is the cost of a lookup, and why?
class ServiceCache: def _index(self, key): return len(key) % 8 # "the hash function is fast!"class ServiceCache { private int index(String key) { return key.length() % 8; // "the hash function is fast!" }}Fix the key
The service crashes on this line. What is the right fix, and why?The service caches every result but never gets a hit. What is the right fix, and why?
cache[[user_id, page]] = result# TypeError: ... unhashable type: 'list'List<Integer> key = new ArrayList<>(List.of(userId, page));cache.put(key, result);key.set(1, page + 1); // the list is reused for the next page: the stored entry is now unreachableRepair the code · line A
Two lines of SimpleHashMap.get are missing. Line A walks the chain and takes each stored key and valuepair in turn. Write it.
def get(self, key): bucket = self.buckets[self._index(key)] ________________________________ # ← line A: walk the chain ____________________________ # ← line B: compare the stored key return value return None public Integer get(String key) { List<Pair> bucket = buckets.get(index(key)); ________________________________ // ← line A: walk the chain ____________________________ // ← line B: compare the stored key return pair.value; } } return null; }Repair the code · line B
Line B compares the stored key with the one being looked up. Write it.
def get(self, key): bucket = self.buckets[self._index(key)] ________________________________ # ← line A: walk the chain ____________________________ # ← line B: compare the stored key return value return None public Integer get(String key) { List<Pair> bucket = buckets.get(index(key)); ________________________________ // ← line A: walk the chain ____________________________ // ← line B: compare the stored key return pair.value; } } return null; }Apply it · the pattern
Find the first word that repeats in a stream of words. Which pattern?
Apply it · complete the solution
Complete the loop: once a word has been checked and is new, remember it.
def first_repeat(stream): seen = set() for word in stream: if word in seen: return word ______________________ # ← remember this word return Nonestatic String firstRepeat(String[] stream) { Set<String> seen = new HashSet<>(); for (String word : stream) { if (seen.contains(word)) { return word; } ______________________ // ← remember this word } return null;}- Find out why a hash map is slow, fix a broken key, repair a chain walk and choose the right pattern.