0
0
Node.jsframework~20 mins

In-memory caching patterns in Node.js - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
In-memory Cache Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this Node.js in-memory cache example?

Consider this simple cache implemented with a Map. What will be logged to the console?

Node.js
const cache = new Map();

function getData(key) {
  if (cache.has(key)) {
    return cache.get(key);
  }
  const value = key * 2;
  cache.set(key, value);
  return value;
}

console.log(getData(3));
console.log(getData(3));
A6\n6
B6\nundefined
C3\n3
Dundefined\n6
Attempts:
2 left
💡 Hint

Think about what happens when the key is already in the cache.

📝 Syntax
intermediate
2:00remaining
Which option correctly implements a TTL (time-to-live) cache entry in Node.js?

We want to store a value in a cache that expires after 1 second. Which code snippet correctly sets and removes the cache entry?

Acache.set(key, value, 1000); // Map does not support TTL natively
Bcache.set(key, value); setInterval(() => cache.delete(key), 1000);
CsetTimeout(() => cache.set(key, value), 1000);
Dcache.set(key, value); setTimeout(() => cache.delete(key), 1000);
Attempts:
2 left
💡 Hint

Map does not support TTL natively. You must remove the key manually after delay.

🔧 Debug
advanced
2:00remaining
Why does this cache implementation cause a memory leak?

Look at this cache code snippet. What causes the memory leak?

Node.js
const cache = {};

function addToCache(key, value) {
  cache[key] = value;
}

// Keys are never removed from cache

addToCache('a', 'data1');
addToCache('b', 'data2');
// ... many keys added over time
AKeys are never removed, so cache grows indefinitely, causing memory leak.
BValues are overwritten causing data loss, not memory leak.
CUsing an object for cache causes syntax errors.
DCache keys are deleted too early causing errors.
Attempts:
2 left
💡 Hint

Think about what happens if you never remove keys from the cache.

state_output
advanced
2:00remaining
What is the final cache content after running this code?

Given this cache with a max size of 2, what keys remain after the operations?

Node.js
class LRUCache {
  constructor(maxSize) {
    this.maxSize = maxSize;
    this.cache = new Map();
  }

  get(key) {
    if (!this.cache.has(key)) return null;
    const value = this.cache.get(key);
    this.cache.delete(key);
    this.cache.set(key, value);
    return value;
  }

  set(key, value) {
    if (this.cache.has(key)) {
      this.cache.delete(key);
    } else if (this.cache.size === this.maxSize) {
      const firstKey = this.cache.keys().next().value;
      this.cache.delete(firstKey);
    }
    this.cache.set(key, value);
  }
}

const cache = new LRUCache(2);
cache.set('a', 1);
cache.set('b', 2);
cache.get('a');
cache.set('c', 3);

console.log([...cache.cache.keys()]);
A['a', 'b']
B['b', 'c']
C['a', 'c']
D['c']
Attempts:
2 left
💡 Hint

Remember that accessing a key moves it to the most recently used position.

🧠 Conceptual
expert
2:00remaining
Which option best explains why WeakMap is useful for in-memory caching?

Why would you choose a WeakMap over a Map for caching objects in Node.js?

AWeakMap stores keys as strings only, making it faster than Map.
BWeakMap allows cached objects to be garbage collected if no other references exist, preventing memory leaks.
CWeakMap automatically persists cache data to disk for durability.
DWeakMap supports TTL natively, removing expired entries automatically.
Attempts:
2 left
💡 Hint

Think about how garbage collection works with weak references.