#450 Time-Limited Key-Value Cache
Write a class that stores key-value pairs where every key has an associated lifetime. After the interval expires, the key must become unavailable.
Class methods:
set(String key, String value, int duration)
get(String key)
count()
-
The
set()method accepts a string key, aStringvalue, and a duration in milliseconds. The key becomes unavailable after the specified duration expires. The method returnstrueif the same unexpired key already exists, andfalseotherwise. The value and duration must be overwritten if the key already exists. -
The
get()method returns the value associated with the key if an unexpired key exists. Otherwise, the method returns-1. -
The
count()method returns the number of keys whose lifetime has not expired.
Question review
Sign in to start the dialogue
The mentor keeps the context of the current question and your previous messages.
Sign in
Available with Premium
This is just one of the solutions available with Premium. And more:
- 250+ advanced lectures
- 1300+ practice exercises
- 500+ solutions to problems from real interviews
- Personalized Java Hub certificates
- Expanded access to the AI mentor