Practice Cache With Time Limit
Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.
Cache With Time Limit
Problem Statement
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. Once the duration has elapsed, the key should be inaccessible. The method should return true if the same un-expired key already exists and false otherwise. Both the value and duration should be overwritten if the key already exists. get(key): if an un-expired key exists, it should return the associated value. Otherwise it should return -1. count(): returns the count of un-expired keys.
Source: Cache With Time Limit on LeetCode. Use the original task for its coding environment and submissions.
Examples
# Example 1
actions = ["TimeLimitedCache", "set", "get", "count", "get"] values = [[], [1, 42, 100], [1], [], [1]] timeDelays = [0, 0, 50, 50, 150]
[null, false, 42, 1, -1]
# Example 2
actions = ["TimeLimitedCache", "set", "set", "get", "get", "get", "count"] values = [[], [1, 42, 50], [1, 50, 100], [1], [1], [1], []] timeDelays = [0, 0, 40, 50, 120, 200, 250]
[null, false, true, 50, 50, -1, 0]
Constraints
Review your practice with PhantomCodeAI
After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.