As reported: eBay MTS-1 online assessment (2025): the CodeSignal Industry Coding Assessment — one codebase, four levels, each level unlocks the next. The task is to complete an in-memory database: level 1 basic operations on records, fields and values (set, compare-and-set, compare-and-delete, get); level 2 a record's fields filtered by prefix (scan, scan by prefix); level 3 TTL for values; level 4 reads of a field's value at a past timestamp. The interface was given in Java with a timestamp as every method's first argument.
Implement a simplified in-memory database. A record is identified by a string key and holds field → value pairs, where field is a string and value an integer. Every operation has a timestamp; timestamps are unique and strictly increasing. Timestamps matter from level 3 on.
Level 1 — set, compare-and-set, compare-and-delete, get
set(timestamp, key, field, value)— inserts the pair, replacing the value if the field exists and creating the record if needed. Returns nothing.compare_and_set(timestamp, key, field, expected_value, new_value) -> bool— sets the field tonew_valueif its current value equalsexpected_value. If the value does not match, or the key or field does not exist, the operation is ignored. Returnstrueif the field was updated.compare_and_delete(timestamp, key, field, expected_value) -> bool— removes the field if its current value equalsexpected_value; otherwise (mismatch, or missing key or field) ignored. Returnstrueif the field was removed.get(timestamp, key, field) -> int | null— the value, ornullif the record or field does not exist.
| call | returns | database |
|---|---|---|
set(1, "A", "B", 4) |
{A: {B: 4}} |
|
set(2, "A", "C", 6) |
{A: {B: 4, C: 6}} |
|
compare_and_set(3, "A", "B", 4, 9) |
true |
{A: {B: 9, C: 6}} |
compare_and_set(4, "A", "C", 4, 9) |
false — C is 6 |
|
compare_and_delete(5, "A", "C", 6) |
true |
{A: {B: 9}} |
get(6, "A", "C") |
null — C was deleted |
|
get(7, "A", "B") |
9 |
Level 2 — scans
scan(timestamp, key) -> list[str]— the record's fields as["<field_1>(<value_1>)", "<field_2>(<value_2>)", ...], sorted lexicographically by field; an empty list if the record does not exist.scan_by_prefix(timestamp, key, prefix) -> list[str]— the same, only fields that start withprefix.
| call | returns |
|---|---|
set(1, "A", "BC", 1), set(2, "A", "BD", 2), set(3, "A", "C", 3) |
|
scan_by_prefix(4, "A", "B") |
["BC(1)", "BD(2)"] |
scan(5, "A") |
["BC(1)", "BD(2)", "C(3)"] |
scan_by_prefix(6, "B", "B") |
[] |
Level 3 — TTL
Each field–value pair can have a time-to-live; the operations of the previous levels write values with an infinite TTL.
set_with_ttl(timestamp, key, field, value, ttl)— likeset, and the pair is available during[timestamp, timestamp + ttl). If the field exists, its value and TTL are updated.ttl > 0.compare_and_set_with_ttl(timestamp, key, field, expected_value, new_value, ttl) -> bool— likecompare_and_set, and the new value gets the TTL.ttl > 0.
An expired field no longer exists for any operation.
| call | returns |
|---|---|
set_with_ttl(1, "A", "B", 5, 10) |
— available during [1, 11) |
set(2, "A", "C", 7) |
|
get(10, "A", "B") |
5 |
get(11, "A", "B") |
null — expired |
set_with_ttl(12, "A", "B", 6, 5) |
— [12, 17) |
compare_and_set_with_ttl(13, "A", "B", 6, 8, 20) |
true — now [13, 33) |
get(20, "A", "B") |
8 |
scan(32, "A") |
["B(8)", "C(7)"] |
scan(33, "A") |
["C(7)"] |
compare_and_set(34, "A", "B", 8, 1) |
false — B has expired |
Level 4 — look-back reads
get_when(timestamp, key, field, at_timestamp) -> int | null— the value the field had at timeat_timestamp. Ifat_timestampis0, performget. It is guaranteed thatat_timestamp <= timestamp. If the record or the field did not exist atat_timestamp, returnnull.
| call | returns |
|---|---|
set(10, "A", "B", 1), set(20, "A", "B", 2) |
|
compare_and_delete(30, "A", "B", 2) |
true |
set_with_ttl(40, "A", "B", 3, 5) |
— [40, 45) |
get_when(50, "A", "B", 15) |
1 |
get_when(51, "A", "B", 20) |
2 — writes at 20 are included |
get_when(52, "A", "B", 30) |
null — deleted at 30 |
get_when(53, "A", "B", 44) |
3 |
get_when(54, "A", "B", 45) |
null — expired |
get_when(55, "A", "B", 5) |
null — did not exist yet |
get_when(56, "A", "B", 0) |
null — same as get(56, …) |
Constraints: at most 3000 operations; 1 <= timestamp <= 10^9; 0 <= value <= 10^6; 1 <= ttl <= 10^6; keys and fields are 1–5 letters or digits.
Code runner format: the first line of stdin is the number of operations q, then one per line: SET t k f v, COMPARE_AND_SET t k f expected new, COMPARE_AND_DELETE t k f expected, GET t k f, SCAN t k, SCAN_BY_PREFIX t k prefix, SET_WITH_TTL t k f v ttl, COMPARE_AND_SET_WITH_TTL t k f expected new ttl, GET_WHEN t k f at. The harness prints - for set / set_with_ttl, true / false, a number or null, and lists as their items separated by spaces (- for an empty list). In C++ and Go, return -1 for a missing value. You only implement the InMemoryDB class.
Clarifications
setandcompare_and_setwrite a value that never expires, even if the field had a TTL before.- A field set or deleted at time
tshows that change inget_when(…, t).