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