Scenario
High-throughput services often suffer from memory bloat due to object overhead and unbounded caches. Build a custom, thread-safe LRU cache with strict memory bounds, optimized with __slots__.
Requirements
- Optimized Data Structures: Compare standard classes vs
@dataclass(slots=True)vs namedtuples. Measure per-instance footprint withsys.getsizeofandpympler. - Thread-Safe LRU Cache: Implement an LRU cache using a doubly linked list + hash map (or
collections.OrderedDict) protected by a reentrant lock (threading.RLock). - Profiling Decorator: Implement
@profile_memory_and_timethat logs elapsed CPU time and heap allocation delta before and after function execution.
Starter Code Layout
build-4-cache-profiler/
├── src/
│ └── memory_tools/
│ ├── __init__.py
│ ├── cache.py # Thread-safe bounded LRU cache
│ ├── models.py # Slots-optimized data structures
│ └── profiler.py # Memory & execution timing decorator
└── tests/
└── test_cache.py
Verification Command
pytest tests/test_cache.py -v
python -m memory_tools.benchmark
100-Point Evaluation Rubric
- Cache Eviction Mechanics (30 pts): Strict O(1) get/put operations and eviction policy.
- Thread Safety (25 pts): Correct use of RLock during eviction and retrieval.
- Memory Optimization (25 pts): Demonstrable >40% memory reduction using
__slots__. - Profiling Tooling (20 pts): Decorator reports accurate timing and allocation deltas.
Grading rubric
Your submission passes when all four hold:
- Works: Cache is thread-safe and bounded; slots/namedtuple savings measured; profiler decorator reports time + heap delta.
- Tested: a pytest suite covers the happy path plus one failure mode, and all tests pass.
- Readable: a README explains setup, the two most important commands, and one design decision you made.
- Production habits: structured logging on failures and no hardcoded secrets/paths (env or config file).
Submission contract
Submit a public repo URL containing source, tests, README, and requirements.txt (or pyproject.toml). A reviewer runs your tests and two commands from your README; if either fails on a clean machine, the submission is returned with notes.