FastPrepIn-Memory Database with TTL and Historical Lookup

In-Memory Database with TTL and Historical Lookup

Persona logoPersona● HardINTERNOA
Learn

Problem statement

Implement an in-memory database that stores integer values under string record keys and string field names. Support conditional changes, sorted scans, per-field expiration, and historical reads. All four levels are combined in this exercise; earlier operations remain available at later levels.

Complete runHistoricalDatabase. Process operations in order and return one string array for each operation. Each row begins with an operation name and a strictly increasing timestamp. Numeric arguments are represented as decimal strings.

Level 1: Values and conditional changes

  • ["SET", timestamp, key, field, value]: create or replace a field with a non-expiring value. Return [].
  • ["GET", timestamp, key, field]: return [value] if the field is visible, otherwise [].
  • ["COMPARE_AND_SET", timestamp, key, field, expectedValue, newValue]: if the visible value equals expectedValue, replace it with the non-expiring newValue and return ["true"]. Otherwise return ["false"] without changing anything.
  • ["COMPARE_AND_DELETE", timestamp, key, field, expectedValue]: delete the field only if its visible value equals expectedValue. Return ["true"] on success and ["false"] otherwise.

Level 2: Ordered scans

  • ["SCAN", timestamp, key]: return all visible fields of the record as strings field(value), ordered lexicographically by field name.
  • ["SCAN_BY_PREFIX", timestamp, key, prefix]: return only visible fields whose names begin with the case-sensitive prefix, in the same format and order.
  • A missing record, no matching fields, or a record with only expired or deleted fields produces [].

Level 3: Time to live

  • ["SET_WITH_TTL", timestamp, key, field, value, ttl]: replace the field with value, visible on the half-open interval [timestamp, timestamp + ttl). Return [].
  • ["COMPARE_AND_SET_WITH_TTL", timestamp, key, field, expectedValue, newValue, ttl]: perform the same visible-value comparison as COMPARE_AND_SET. On success the new value expires at timestamp + ttl; return ["true"]. A failed comparison changes neither value nor expiration and returns ["false"].
  • Every current read, scan, comparison, and deletion treats an expired field as absent. Successful non-TTL writes remove any previous TTL.

Level 4: Historical lookup

  • ["GET_WHEN", timestamp, key, field, atTimestamp]: return the value visible at atTimestamp as [value], or [] if no value was visible then.
  • If atTimestamp is 0, perform a current GET at timestamp.
  • Overwrites and successful deletions take effect at their timestamp but preserve earlier history. A historical read at an exact mutation timestamp observes that mutation.
  • Expiration never restores an older overwritten value. A read first considers the most recent successful mutation at or before the requested time; if that version is deleted or expired, return [].
  • Failed conditional operations create no version and have no effect on later historical reads.

Function

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

Examples

Example 1

operations = [["SET_WITH_TTL","1","A","B","3","10"],["COMPARE_AND_SET_WITH_TTL","4","A","B","3","7","9"],["GET","10","A","B"],["GET_WHEN","13","A","B","3"],["GET_WHEN","15","A","B","13"]]return = [[],["true"],["7"],["3"],[]]

The first value is visible from time 1 until the overwrite at 4. The replacement is visible on [4,13). The historical read at 3 returns 3; the read at its exact expiration time 13 returns no value.

Example 2

operations = [["SET","1","user","age","18"],["SET","2","user","address","1"],["SET","3","user","height","180"],["SCAN","4","user"],["SCAN_BY_PREFIX","5","user","a"],["COMPARE_AND_DELETE","6","user","age","18"],["GET","7","user","age"],["GET_WHEN","8","user","age","5"]]return = [[],[],[],["address(1)","age(18)","height(180)"],["address(1)","age(18)"],["true"],[],["18"]]

Scans sort by field name; the prefix scan selects address and age. The successful deletion hides the current age but does not erase its earlier value.

Constraints

  • 1 <= operations.length <= 2000.
  • Each row has exactly the arguments shown for its operation.
  • Operation timestamps are integers in [1, 10^9] and strictly increase.
  • 0 <= atTimestamp <= timestamp.
  • 1 <= ttl <= 10^9.
  • Values and expected values are canonical signed 32-bit decimal integers.
  • Keys, fields, and prefixes contain 1 through 50 case-sensitive English letters or digits.
  • The database is empty before the first operation.

More Persona problems

See Persona hiring insights
public String[][] runHistoricalDatabase(String[][] operations) {
  // Write your code here.
}
operations[["SET_WITH_TTL","1","A","B","3","10"],["COMPARE_AND_SET_WITH_TTL","4","A","B","3","7","9"],["GET","10","A","B"],["GET_WHEN","13","A","B","3"],["GET_WHEN","15","A","B","13"]]
expected[[], ["true"], ["7"], ["3"], []]
Checking account…