← Questions

Thread-safe job registry with expiry

0
MetaCodingLast asked Nov 2025

A coding interview question asked in Meta's AI-enabled coding round, last asked Nov 2025. It tests concurrency, hash maps and ttl.

Build an in-memory registry of jobs that many threads read and write at the same time. Each job is registered with a time to live. A lookup must never return a job whose time has passed, and expired jobs must be cleaned up without blocking callers for long.

Interface

Unless you agree on a different interface with the interviewer, assume:

  • JobRegistry(clock=time.monotonic) — the clock is injectable so tests can control time.
  • register(job_id: str, job: object, ttl_seconds: float) -> None — adds or replaces a job. It expires at now + ttl_seconds.
  • get(job_id: str) -> object | None — returns the job, or None if it is unknown or has expired.
  • remove(job_id: str) -> bool — removes a live job. Returns False if it was absent or already expired.
  • size() -> int — the number of live jobs.

All methods must be safe to call from many threads.

Example

r = JobRegistry(clock=fake)           # fake clock starts at 0
r.register("a", "build", 10)          # expires at 10
r.register("b", "test", 5)            # expires at 5
r.get("b")       -> "test"
fake.advance(6)
r.get("b")       -> None              # expired at 5
r.size()         -> 1
r.register("a", "build-2", 10)        # replaces "a", now expires at 16
fake.advance(5)
r.get("a")       -> "build-2"         # time is 11

Constraints

  • Reads are much more frequent than writes.
  • Up to millions of jobs. No single call may pause for a full scan of the registry.

Clarifying questions

  • Does re-registering a job reset its expiry?
  • Is a job at exactly its expiry time live or expired?
  • Must size() be exact, or can it be eventually correct?
  • Do callers need a callback when a job expires?

What a strong answer covers

  • A dictionary from id to (job, expires_at), guarded by a lock. Then a refinement: a read-write lock or lock striping, so readers rarely contend.
  • Expiry enforced lazily on every read, so correctness never depends on the cleaner.
  • Cleanup by a min-heap of (expires_at, job_id) or a timing wheel, drained in small batches by a background thread or on writes. Stale heap entries are skipped when the job was re-registered.
  • Why the lock is held only for short critical sections, and never while running user callbacks.
  • Tests with a fake clock: expiry boundaries, re-registration, concurrent register and get under many threads, and a shutdown path for the sweeper thread.

Follow-ups

  • Add extend(job_id, ttl) without a race between the read and the write.
  • Notify listeners when a job expires, without holding the lock during the callback.
  • Make the registry shared across several processes or machines.

Where it's asked

  • Meta · AI-enabled coding · Last asked Nov 202560 min · Several progressive tasks on a starter codebase in CoderPad with an AI assistant panel; usually two rounds · The first task usually without AI, later tasks with it

What it tests

concurrency · hash maps · ttl

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding