#450 Time-Limited Key-Value Cache

Companies:
TechIntel
Themes:
JavaCollections & GenericsPractice

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, a String value, and a duration in milliseconds. The key becomes unavailable after the specified duration expires. The method returns true if the same unexpired key already exists, and false otherwise. 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

Mentor
Answer as you would in a real interview. I’ll ask follow-up questions, then compare your answer with the reference answer and show your accuracy as a %.
Sign in to start the dialogue The mentor keeps the context of the current question and your previous messages.
Sign in