DataDriven
LearnPracticeInterviewDiscussDailyJobs

The Eviction Policy

A medium Python interview practice problem on DataDriven. Write and execute real python code with instant grading.

Domain
Python
Difficulty
medium
Seniority
L4

Problem

Implement an LRU (least recently used) cache and drive it from a list of operations. The cache supports two methods, both O(1): - get(key): return the stored value, or -1 if the key is absent. A successful get marks the key as most recently used. - put(key, value): insert or update the key, marking it most recently used. If this exceeds the fixed capacity, first evict the least recently used entry. Write the top-level driver `the_eviction_policy(operations)`. `operations` is a list of operations to replay in order. The first operation is always ['LRUCache', capacity] (1 <= capacity <= 1000); each subsequent operation is either ['put', key, value] or ['get', key]. Return a list of results, one per operation, in order: None for the constructor and for every put, and for each get the stored value or -1 if the key is absent. For example, with capacity 2: put(1,1), put(2,2), get(1) -> 1, put(3,3) evicts key 2, get(2) -> -1. So the driver returns [None, None, None, 1, None, -1].

Summary

Fixed capacity. Oldest unused entry gets evicted.

Practice This Problem

Solve this Python problem with real code execution. DataDriven runs your Python code in a real environment and grades it automatically.

Related

  • All Practice Problems
  • Mock Interview Mode
  • Python Interview Questions
  • Data Engineering Interview Prep Guide
  • Daily Challenge
  • Data Engineering Lessons