Skip to main content
HardBackendDebug

Fix the Broken Cache

Fix a production bug in a multi-file Python in-memory cache so that all provided tests pass. The repo implements `get_user_profile(user_id)`, which fetches from an upstream client and caches with a TTL. The cache curren...

What you will practice

PythonCachingSecurityDebuggingTestingMutability

Requirements

  • Keep the public `get_user_profile(user_id)` API intact.
  • Cache profiles independently per user id.
  • Repeated reads for the same user within the TTL must not hit upstream again.
  • A profile must be fetched again after its TTL expires.
  • Mutating a returned profile must not corrupt the cached value.
  • `update_user_profile(user_id, data)` must invalidate that user's cached profile.
  • Do not modify the provided test files.

Starter files

profile_service.pyEditable starter
cache.pyEditable starter
client.pyEditable starter

What the judge checks

  • Runs in the python environment with the python-pytest runner.
  • Uses a 5000ms judge budget.
  • Behavior rules include: No Test Modifications, Deterministic Time Required.

Constraints

  • Use Python standard library only
  • You may edit only non-test source files

Example behavior

Input
Call get_user_profile("u1") twice within TTL
Output
Second call returns cached data without calling the upstream client again
Input
p = get_user_profile("u1")
p["name"] = "Hacked"
Call get_user_profile("u1") again within TTL
Output
The returned profile must not include "Hacked"

Caller mutation must not affect the cache.

Follow-up

How would you adapt this cache to be safe under concurrent access from multiple threads?