DesignTheSystem/dev
MEDIUM Low-Level Design (LLD) 11 automated tests

Design Key-Value Store with Expiry (Low-Level Design)

An in-memory store with per-key expiry, later with nested transactions. Focus: Time-based expiry, then layered transactions.

DifficultyMEDIUM
Design focusTime-based expiry, then layered transactions
Automated grading3 public + 8 hidden tests (11 total)
Phase 2 follow-upTransactions
LanguageTypeScript (OOP / Machine Coding)

Problem statement

Store string values by key. A value may have a time to live (ttlMs): it expires at now + ttlMs and after that behaves as if it was never set. Without ttlMs it never expires.

Time is passed in as `now` (milliseconds) so tests control the clock. Setting a key again replaces its value and its expiry.

Functional requirements

  • set(key, value, now, ttlMs?); a ttlMs that is not positive throws.
  • get(key, now) returns the value, or null if missing or expired (a key is expired when now ≥ its expiry).
  • delete(key) returns true if a key was removed. count(now) counts keys that are not expired.

Required API & starter interface

Implement the TypeScript class interface below. You can add any helper classes, policies or strategies you need:

class KeyValueStore {
  set(key: string, value: string, now: number, ttlMs?: number): void {
    throw new Error("Not implemented");
  }

  get(key: string, now: number): string | null {
    throw new Error("Not implemented");
  }

  delete(key: string): boolean {
    throw new Error("Not implemented");
  }

  count(now: number): number {
    throw new Error("Not implemented");
  }
}

Public test cases

Your code runs against 3 public tests plus 8 hidden edge-case tests in a sandbox:

Set and get (core)

var s = new KeyValueStore();
s.set("a", "1", 0);
assertEqual(s.get("a", 5), "1");
assertEqual(s.get("b", 5), null);

Values expire (core)

var s = new KeyValueStore();
s.set("a", "1", 100, 50);
assertEqual(s.get("a", 149), "1");
assertEqual(s.get("a", 150), null);

Bad time to live (edge)

var s = new KeyValueStore();
assertThrows(function () { s.set("a", "1", 0, 0); });

Design & structure checks

Beyond passing tests, the grader inspects your class structure for clean object-oriented design:

  • A stored value and its expiry travel together (an entry type): Keeping value and expiry in one record means they are always set, copied and rolled back together.

Phase 2: Change request

In real interviews, the follow-up tests whether your design is extensible. After your first version passes all 11 tests, you unlock the change request: Transactions.

Pair with High-Level System Design

Ready to scale Key-Value Store with Expiry across distributed servers? Try the matching HLD simulator problem: Design Key-Value Store.

Relevant low-level design lessons

More low-level design problems