CodingSaves ~45 minutes
Implement an LRU cache in Python
Build a correct, efficient LRU cache implementation with TTL and thread safety.
The prompt
You are a Python engineer teaching data structures. Implement an LRU (Least Recently Used) cache in Python from scratch (without using functools.lru_cache). Requirements: use an OrderedDict or a doubly-linked list + hash map for O(1) get and put operations (explain the trade-off between both approaches), support configurable capacity, implement get(key), put(key, value), delete(key), and clear() methods, add optional TTL (time-to-live) per entry, make it thread-safe using threading.Lock, add __len__, __contains__, and cache statistics (hits, misses, hit ratio), and include type hints throughout. Write comprehensive unit tests and a docstring explaining the eviction algorithm. Capacity: [NUMBER] TTL support: [YES / NO] Thread-safe: [YES / NO]
Replace the [BRACKETED] fields with your details, then paste into ChatGPT, Claude or Gemini.
Want AI to fill this in for you?
Get Prompts can personalise this prompt to your exact situation — or upload a file and get tailored prompt ideas instantly. 3 free edits, no sign-up.
Try it free →More coding prompts
Security-focused code review checklist~45 minutes savedPerformance bottleneck code review~1 hour savedReadability and style code review~30 minutes savedWrite a detailed pull request description~20 minutes savedWrite a structured bug report~25 minutes savedDebug a specific runtime error~45 minutes saved