Complete the code to identify the cache eviction policy that removes the least recently used item.
if cache.is_full(): cache.evict_policy = "[1]"
The LRU (Least Recently Used) policy evicts the item that was used the longest time ago.
Complete the code to set the cache eviction policy that removes the least frequently used item.
if cache.needs_eviction(): cache.policy = "[1]"
LFU (Least Frequently Used) evicts the item with the lowest access frequency.
Fix the error in the code to correctly implement a time-based cache eviction policy.
cache.set_eviction_policy("[1]") cache.expiry_time = 300 # seconds
TTL (Time To Live) evicts items after a set time expires.
Fill both blanks to complete the dictionary comprehension that filters cache entries by TTL and sorts by usage frequency.
filtered_cache = {k: v for k, v in cache.items() if v.expiry > [1]
sorted_cache = sorted(filtered_cache.items(), key=lambda item: item[1].[2])We filter entries where expiry is greater than current time, then sort by frequency for LFU.
Fill all three blanks to complete the cache eviction function that uses LRU policy with TTL expiration.
def evict_cache(cache): now = [1] valid_items = {k: v for k, v in cache.items() if v.expiry > now} lru_key = min(valid_items, key=lambda k: valid_items[k].[2]) del cache[lru_key] return cache current_time = [3]
The function uses current time from time.time(), filters valid items by expiry, finds the least recently accessed key, and deletes it.