Code Room
CodingHardcod-g971
Subject Cache storageLevel Senior~32 minCommon in Storage & CDN interviewsIndustries Technology, Software development

Question

Implement a cache that combines LRU eviction with per-entry TTL. Process operations: ["put", now, key, value, ttl] inserts/updates key=value at time `now`, expiring at now+ttl (alive while query time < now+ttl), and marks it most-recently-used; ["get", now, key] returns the value if present and not expired (and marks it most-recently-used), else -1. Capacity is `capacity`: when a put would exceed capacity with a new key, first drop any currently-expired entries (as of `now`); if still over capacity, evict the least-recently-used live entry. A put that updates an existing key never triggers capacity eviction. Timestamps are non-decreasing. Return the list of get results in order. capacity is a positive integer.

Implement
lru_ttl_cache(capacity: int, ops: list[list]) → list[int]
Examples
in[2,[["put",0,"a",1,100],["put",0,"b",2,100],["get",1,"a"],["put",1,"c",3,100],["get",2,"b"],["get",2,"c"]]]out[1,-1,3]
What a strong answer looks like

State your approach and its time/space complexity out loud before you optimize. Handle the edge cases (empty input, duplicates, overflow), and say why you chose this over the brute force. Green tests are the floor, not the grade.

Vibe coding: describe the solution in plain language (or narrate it) and the coach grades your approach. Generating runnable code from your description is coming next.

Run or narrate your approach, then ask the coach.