Problem · Hash Table

In-Memory Database with TTL

Learn this problem
MediumAutodesk logoAutodeskFULLTIMEOA

Problem statement

Process a finite batch of timestamped operations on an in-memory key-value database. Timestamps are nondecreasing, and rows with the same timestamp are processed in input order. An entry set at time t with TTL d is active while now < t + d.

OperationReturned string
["SET", key, value, ttl, now]Store the value and expiration. Return true if the key had an active value immediately before this operation, otherwise false.
["GET", key, now]Return the active value, or NULL.
["COUNT", now]Return the number of active keys.

Expired entries do not count as existing and may be removed lazily.

Function

runTimedDatabase(operations: String[][]) → String[]

Examples

Example 1

operations = [["SET","a","1","5","0"],["GET","a","4"],["GET","a","5"],["COUNT","5"]]return = ["false","1","NULL","0"]

Key a is active before time 5 and expires exactly at time 5.

Example 2

operations = [["SET","x","old","10","1"],["SET","x","new","3","4"],["GET","x","6"],["SET","x","fresh","5","7"],["COUNT","7"]]return = ["false","true","new","false","1"]

The first overwrite finds x active. The second value expires at time 7, so the next set reports false and creates a fresh active entry.

Constraints

  • 1 <= operations.length <= 200000.
  • Keys and values are non-empty ASCII strings and values are never NULL.
  • Timestamps are nondecreasing integers from 0 through 10^18.
  • Every TTL is an integer from 1 through 10^18.

More Autodesk problems

drafts saved locally
public String[] runTimedDatabase(String[][] operations) {
    // Write your code here.
}
operations[["SET","a","1","5","0"],["GET","a","4"],["GET","a","5"],["COUNT","5"]]
expected["false", "1", "NULL", "0"]
checking account