Practice/Anthropic/In-memory Database (Online Assessment)
CodingMust
You are building a simplified in-memory database system. This is a multi-level online assessment problem with progressively increasing complexity:
Subsequent levels are unlocked when the current level is correctly solved. You always have access to the data for the current and all previous levels.
Input: List of queries (array of string arrays) to the system
Output: Array of strings representing the returned values of all queries
Note: Each query calls one operation. Operations are given in order of strictly increasing timestamps.
key (string)Implement basic CRUD operations for records with field-value pairs.
SET timestamp key field value
Inserts a field-value pair to the record associated with key. If the field already exists, replace its value. Returns empty string "".
COMPARE_AND_SET timestamp key field expectedValue newValue
Updates the value of field to newValue only if the current value equals expectedValue. If the expected value doesn't match, or if key/field doesn't exist, the operation is ignored. Returns "true" if updated, "false" otherwise.
COMPARE_AND_DELETE timestamp key field expectedValue
Removes the field only if the current value equals expectedValue. Returns "true" if removed, "false" otherwise.
GET timestamp key field
Returns a string representing the value of the field. Returns empty string "" if record or field doesn't exist.
Queries:
[ ["SET", "0", "A", "B", "4"], ["SET", "1", "A", "C", "6"], ["COMPARE_AND_SET", "2", "A", "B", "4", "9"], ["COMPARE_AND_SET", "3", "A", "C", "4", "9"], ["COMPARE_AND_DELETE", "4", "A", "C", "6"], ["GET", "5", "A", "C"], ["GET", "6", "A", "B"] ]
Level 1 ExampleQueryResultDatabase StateSET 0 A B 4""{"A": {"B": 4}}SET 1 A C 6""{"A": {"B": 4, "C": 6}}COMPARE_AND_SET 2 A B 4 9"true"{"A": {"B": 9, "C": 6}}COMPARE_AND_SET 3 A C 4 9"false"unchanged (C is 6, not 4)COMPARE_AND_DELETE 4 A C 6"true"{"A": {"B": 9}}GET 5 A C""(field C deleted)GET 6 A B"9"
Output: ["", "", "true", "false", "true", "", "9"]
Support displaying record data based on filters.
SCAN timestamp key
Returns a comma-separated string representing all fields of the record, sorted lexicographically.
Format: "field_1(value_1), field_2(value_2), ..."
Returns empty string if record doesn't exist.
SCAN_BY_PREFIX timestamp key prefix
Returns fields starting with prefix, sorted lexicographically. Same format as SCAN.
Returns empty string if record doesn't exist or no fields match.
Queries:
[ ["SET", "1", "A", "BC", "1"], ["SET", "2", "A", "BD", "2"], ["SET", "3", "A", "C", "3"], ["SCAN_BY_PREFIX", "4", "A", "B"], ["SCAN", "5", "A"], ["SCAN_BY_PREFIX", "6", "B", "B"] ]
Level 2 ExampleQueryResultExplanationSET 1 A BC 1""Database: {"A": {"BC": 1}}SET 2 A BD 2""Database: {"A": {"BC": 1, "BD": 2}}SET 3 A C 3""Database: {"A": {"BC": 1, "BD": 2, "C": 3}}SCAN_BY_PREFIX 4 A B"BC(1), BD(2)"Fields starting with "B"SCAN 5 A"BC(1), BD(2), C(3)"All fields in record ASCAN_BY_PREFIX 6 B B""Record B doesn't exist
Output: ["", "", "", "BC(1), BD(2)", "BC(1), BD(2), C(3)", ""]
"field(value), field(value), ..."""Support TTL settings for fields. Each field-value pair can have a TTL that determines how long it persists before automatic removal.
timestamp with duration ttl exists during the interval [timestamp, timestamp + ttl)timestamp + ttl, the field expires and is no longer accessibleSET_WITH_TTL timestamp key field value ttl
Inserts or updates a field-value pair with Time-To-Live. The field exists during [timestamp, timestamp + ttl). Returns empty string "".
All previous operations now respect TTL expiration:
Queries:
[ ["SET_WITH_TTL", "1", "A", "BC", "1", "9"], ["SET_WITH_TTL", "5", "A", "BC", "2", "10"], ["SET", "6", "A", "BD", "3"], ["SCAN_BY_PREFIX", "14", "A", "B"], ["SCAN_BY_PREFIX", "15", "A", "B"] ]
Level 3 Example 1QueryResultExplanationSET_WITH_TTL 1 A BC 1 9""BC expires at t=10SET_WITH_TTL 5 A BC 2 10""BC overwritten, now expires at t=15SET 6 A BD 3""BD has no TTL (never expires)SCAN_BY_PREFIX 14 A B"BC(2), BD(3)"Both fields exist at t=14SCAN_BY_PREFIX 15 A B"BD(3)"BC expired at t=15
Output: ["", "", "", "BC(2), BD(3)", "BD(3)"]
Queries:
[ ["SET", "1", "A", "B", "1"], ["SET_WITH_TTL", "2", "X", "Y", "5", "15"], ["GET", "3", "X", "Y"], ["SET_WITH_TTL", "4", "A", "D", "2", "10"], ["SCAN", "13", "A"], ["SCAN", "14", "A"], ["SCAN", "16", "X"], ["SCAN", "17", "X"], ["COMPARE_AND_DELETE", "20", "X", "Y", "5"] ]
Level 3 Example 2QueryResultExplanationSET 1 A B 1""B never expiresSET_WITH_TTL 2 X Y 5 15""Y expires at t=17GET 3 X Y"5"Y existsSET_WITH_TTL 4 A D 2 10""D expires at t=14SCAN 13 A"B(1), D(2)"Both existSCAN 14 A"B(1)"D expired at t=14SCAN 16 X"Y(5)"Y still existsSCAN 17 X""Y expired at t=17COMPARE_AND_DELETE 20 X Y 5"false"Y already expired
Output: ["", "", "5", "", "B(1), D(2)", "B(1)", "Y(5)", "", "false"]
timestamp + ttlSupport retrieving historical values — query what value a field had at a specific timestamp in the past.
GET_WHEN timestamp key field at_timestamp
Returns the value of field at at_timestamp. Important points:
at_timestamp ≤ timestampat_timestamp is 0, performs a regular GET operation at the current timestampat_timestampQueries:
[ ["SET_WITH_TTL", "1", "A", "B", "10", "5"], ["GET", "2", "A", "B"], ["SET", "3", "A", "B", "20"], ["GET_WHEN", "10", "A", "B", "2"], ["GET_WHEN", "10", "A", "B", "7"], ["GET", "10", "A", "B"] ]
Level 4 ExampleQueryResultExplanationSET_WITH_TTL 1 A B 10 5""B = 10, expires at t=6GET 2 A B"10"B exists at t=2SET 3 A B 20""B = 20, no TTL (never expires)GET_WHEN 10 A B 2"10"At t=2, B had value 10GET_WHEN 10 A B 7"20"At t=7, B had value 20 (old value expired at t=6, new value set at t=3)GET 10 A B"20"Current value is 20
Output: ["", "10", "", "10", "20", "20"]
at_timestamp