TTL Cache

Asked at Netflix· Screen

Reported by the problem's creator, not verified

Import another question

Design an in-memory cache keyed by strings, holding integer values. Every method takes the current timestamp at as its first argument, and timestamps never decrease between calls. Each test drives one TTLCache() instance through a sequence of calls and records every call's return value (None for methods that return nothing).

Examples

  1. Example 1

    Input: init = [], operations = [["put",[1,"a",10,5]],["get",[2,"a"]],["get",[5,"a"]],["get",[6,"a"]],["count",[6]],["put",[7,"b",0,3]],["get",[8,"b"]],["put",[9,"b",7,1]],["get",[9,"b"]],["get",[10,"b"]],["count",[10]]]

    Output: [null,10,10,null,0,null,0,null,7,null,0]

    Explanation: a is stored at time 1 with ttl 5, so it is live through time 5 and expired at 6. b is stored at 7, replaced at 9 with a one-unit ttl, readable at 9 and gone at 10. count sees only live entries.

Import your own questions
Paste a question you were asked and get a full problem: statement, tests and a checked solution.
Import a question

Code

Loading editor…

Ln 1, Col 1

Start an attempt to use Coach.