FastPrepExpiring Key Set
Problem · Design

Expiring Key Set

Learn this problem
HardAmazon logoAmazonNEW GRADONSITE INTERVIEW
See Amazon hiring insights

Problem statement

Simulate a set of string keys over time. The aligned arrays describe operations in nondecreasing timestamp order:

  • add: insert or refresh keys[i]. It expires at timestamps[i] + ttl[i].
  • contains: append whether keys[i] is currently present.
  • remove: delete keys[i] immediately if present.

At the start of every operation, delete entries whose expiration time is less than or equal to the current timestamp. For non-add operations, ttl[i] is zero. Return the answers to contains operations in encounter order.

Function

runExpiringSet(operations: String[], keys: String[], timestamps: int[], ttl: int[]) → boolean[]

Examples

Example 1

operations = ["add","contains","contains"]keys = ["a","a","a"]timestamps = [0,4,5]ttl = [5,0,0]return = [true,false]

The key is present before time 5 and absent exactly at its expiration time.

Example 2

operations = ["add","add","contains","contains"]keys = ["job","job","job","job"]timestamps = [0,2,3,7]ttl = [3,5,0,0]return = [true,false]

Refreshing at time 2 replaces expiration 3 with expiration 7.

Example 3

operations = ["add","contains","remove","contains"]keys = ["x","x","x","x"]timestamps = [1,2,3,3]ttl = [10,0,0,0]return = [true,false]

Removal takes effect for a later operation at the same timestamp.

Constraints

  • 1 <= operations.length = keys.length = timestamps.length = ttl.length <= 100000.
  • Operations are add, contains, or remove.
  • Keys are nonempty alphanumeric strings of length at most 40.
  • 0 <= timestamps[i] <= 2 * 10^9 and timestamps are nondecreasing.
  • For add, 1 <= ttl[i] <= 10^9; otherwise ttl[i] == 0.

More Amazon problems

drafts saved locally
public boolean[] runExpiringSet(String[] operations, String[] keys, int[] timestamps, int[] ttl) {
  // write your code here
}
operations["add","contains","contains"]
keys["a","a","a"]
timestamps[0,4,5]
ttl[5,0,0]
expected[true,false]
checking account