Code RoomLRU cache with TTL
HardPrep Room Coding #1534

LRU cache with TTL

CodingStorage & CDNSenior~32 min

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.

0:00 of about 32 min
InputExpectedGot
[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"]]][1,-1,3]not run yetsample