HardJavaScriptPractice mode: untimed, 3 hints
User cache
A tiny user store with a read cache in front of it. getUser(id) returns { id, name } or null; renameUser(id, name) changes the stored name and returns true, or false for an unknown id. A read after a rename must see the new name.
The code has exactly one planted bug. Reproduce it with the tests, fix it with the smallest change, then submit.
Visible tests
read after rename
- getUser(1) → {"id":1,"name":"Asha"}
- renameUser(1, "Asha K") → true
- getUser(1) → {"id":1,"name":"Asha K"}
unknown user
- getUser(3) → null
Hints 0/3
// A tiny user store with a read cache in front of it.
const db = new Map([
[1, { id: 1, name: "Asha" }],
[2, { id: 2, name: "Ravi" }],
]);
const cache = new Map();
function getUser(id) {
if (!cache.has(id)) {
const row = db.get(id);
cache.set(id, row ? { ...row } : null);
}
return cache.get(id);
}
function renameUser(id, name) {
const row = db.get(id);
if (!row) return false;
db.set(id, { ...row, name });
return true;
}
Ctrl/⌘+Enter runs, Ctrl/⌘+Shift+Enter submits. In the editor, Ctrl+M switches Tab to moving focus.
Run works without an account. Log in to submit against the hidden tests and track your accuracy per bug type.
Ready for pressure? Try a timed set: six snippets, 20 minutes, no hints.