FastPrepTime-Based Key-Value Store with Range Aggregates

Time-Based Key-Value Store with Range Aggregates

Confluent logoConfluent● MediumFULLTIMEPHONE SCREEN
Learn

Problem statement

Process a sequence of operations on a time-based key-value store. Each key keeps integer values at strictly increasing integer timestamps.

  • SET key timestamp value: store the value and return "null".
  • GET key timestamp: return the value at the greatest stored timestamp not exceeding timestamp, or the empty string when no such value exists.
  • SUM key start end: return the sum of values whose timestamps are in the inclusive interval [start, end], or "0" when the interval is empty.
  • AVG key start end: return the arithmetic mean over the same inclusive interval as an exact reduced fraction. Return an integer when the denominator is one, and the empty string when the interval is empty.

Return one string for every operation, in input order.

Function

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

Examples

Example 1

operations = ["SET cpu 1 10","SET cpu 3 20","GET cpu 2","SUM cpu 1 3","AVG cpu 1 3","GET cpu 0","SUM cpu 4 8","AVG cpu 4 8"]return = ["null","null","10","30","15","","0",""]

The lookup at timestamp 2 sees the value 10. Both values lie in [1,3], so the sum is 30 and the average is 15. The remaining queried ranges contain no eligible values.

Example 2

operations = ["SET load 2 -4","SET load 5 7","SET load 9 2","GET load 8","SUM load 3 9","AVG load 2 9","AVG load 5 9"]return = ["null","null","null","7","9","5/3","9/2"]

The range [2,9] contains all three values, whose exact average is 5/3. The range [5,9] contains 7 and 2, whose average is 9/2.

Constraints

  • 1 <= operations.length <= 100000.
  • Every operation has one of the four documented forms.
  • Keys contain 1 to 30 lowercase English letters or digits.
  • 0 <= timestamp, start, end <= 10^9 and start <= end.
  • -10^9 <= value <= 10^9.
  • SET timestamps are strictly increasing for each key.
  • Every range sum fits in a signed 64-bit integer.

More Confluent problems

See Confluent hiring insights
public String[] runTimeStore(String[] operations) {
    // Write your solution here.
}
operations["SET cpu 1 10","SET cpu 3 20","GET cpu 2","SUM cpu 1 3","AVG cpu 1 3","GET cpu 0","SUM cpu 4 8","AVG cpu 4 8"]
expected["null", "null", "10", "30", "15", "", "0", ""]
Checking account…