FastPrepTime-Indexed Key-Value Store

Time-Indexed Key-Value Store

Together AI logoTogether AI● MediumFULLTIMEONSITE INTERVIEW
Learn

Problem statement

Process a finite sequence of operations on an initially empty time-indexed key-value store.

  • ["SET", key, timestamp, value] stores value for key at the non-negative integer timestamp. Setting the same key and timestamp again replaces the previous value.
  • ["GET", key, timestamp] returns the value stored for that key at the greatest timestamp less than or equal to the query timestamp. Return the empty string when no such value exists.

Operations arrive in the given order, but timestamps are not necessarily ordered. Return the results of the GET operations in encounter order; SET operations produce no output.

Function

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

Examples

Example 1

operations = [["SET","model","4","v1"],["SET","model","9","v2"],["GET","model","7"],["GET","model","9"]]return = ["v1","v2"]

The first query uses timestamp 4; the second can use the exact timestamp 9.

Example 2

operations = [["SET","k","10","late"],["SET","k","2","early"],["GET","k","5"],["GET","missing","99"]]return = ["early",""]

Out-of-order timestamps are indexed normally, and an unknown key returns the empty string.

Example 3

operations = [["SET","a","3","old"],["SET","a","3","new"],["GET","a","3"]]return = ["new"]

The later write replaces the value at the same key and timestamp.

Constraints

  • 1 <= operations.length <= 10^4.
  • Each row is a valid SET or GET operation in the format above.
  • Keys and values contain 1 to 30 printable ASCII characters and do not contain the empty string.
  • 0 <= timestamp <= 10^9.

More Together AI problems

See Together AI hiring insights
public String[] timeMap(String[][] operations) {
    // Write your solution here.
}
operations[["SET","model","4","v1"],["SET","model","9","v2"],["GET","model","7"],["GET","model","9"]]
expected["v1", "v2"]
Checking account…