HardPro challengePythonJavaScriptTypeScript

LRU Cache with TTL

Node.jsCacheAlgorithms

LRU + per-entry TTL. solve(capacity, ttl, ops) runs operations:

  • ['set', now, key, value]
  • ['get', now, key] returns value or null. Expired entries return null and are evicted.

Returns array of get results (in order).

Sample tests

Test #1Boundary
Input: [1,50,[["set",0,"k","v"],["get",49,"k"],["get",50,"k"]]]
Output: ["v",null]
Test #2Within TTL
Input: [2,100,[["set",0,"a",1],["set",0,"b",2],["get",50,"a"]]]
Output: [1]
Test #3Expired
Input: [2,100,[["set",0,"a",1],["get",200,"a"]]]
Output: [null]
Test #4LRU evicted a
Input: [2,100,[["set",0,"a",1],["set",0,"b",2],["set",0,"c",3],["get",0,"a"]]]
Output: [null]
Test #5Touch a, evict b
Input: [2,100,[["set",0,"a",1],["set",0,"b",2],["get",0,"a"],["set",0,"c",3],["get",0,"b"]]]
Output: [1,null]