Cache With Time Limit — LeetCode 2622 Python Solution

MediumJavaScript
Problem
#2622
Reading time
2 min

The problem

Write a class that allows getting and setting key-value pairs, however a time until expiration is associated with each key. The class has three public methods: set(key, value, duration): accepts an integer key, an integer value, and a duration in milliseconds.

Example

Input
actions = ["TimeLimitedCache", "set", "get", "count", "get"]
Output
[null, false, 42, 1, -1]
Explanation
At t=0, the cache is constructed.

Complexity

MeasureComplexity
TimeO(n)
SpaceO(1) to O(n) auxiliary

Related problems

Frequently asked questions

How hard is LeetCode 2622. Cache With Time Limit?
LeetCode 2622. Cache With Time Limit is rated Medium on LeetCode.
What topics does LeetCode 2622. Cache With Time Limit cover?
LeetCode 2622. Cache With Time Limit is tagged JavaScript on LeetCode.

Stuck on problems like this in a live interview?

Stealth Interview is a desktop app for macOS and Windows. It reads the problem off your screen and returns a working solution with a step-by-step explanation and its time and space complexity — invisible to screen sharing.

Get Stealth Interview