Coinbase OA: In-Memory DB – Full Solution & Examples

25 Views
No Comments
Coinbase OA: In-Memory DB – Full Solution & Examples

Level 1

The basic level of the in-memory database contains records. Each record can be accessed with a unique identifier key of string type. A record may contain several field - value pairs, both of which are of string type.

Function Definitions

  • set(self, key: str, field: str, value: str) -> None
    — should insert a field - value pair to the record associated with key. If the field in the record already exists, replace the existing value with the specified value. If the record does not exist, create a new one.
  • get(self, key: str, field: str) -> str | None
    — should return the value contained within field of the record associated with key. If the record or the field doesn’t exist, should return None.
  • delete(self, key: str, field: str) -> bool
    — should remove the field from the record associated with key. Returns True if the field was successfully deleted, and False if the key or the field do not exist in the database.

Examples

Queries Explanations
set("A", "B", "E") database state: {“A”: {“B”:“E”}}
set("A", "C", "F") database state: {“A”: {“C”:“F”,“B”:“E”}}
get("A", "B") returns“E”
get("A", "D") returns None
delete("A", "B") returns True; database state: {“A”: {“C”:“F”}}
delete("A", "D") returns False; database state: {“A”: {“C”:“F”}}

Level 2

The database should support displaying data based on filters. Introduce an operation to support printing some fields of a record.

Function Definitions

  • scan(self, key: str) -> list[str]
    — should return a list of strings representing the fields of a record associated with key.
    The returned list should be in the format
    ["<field_1>(<value_1>)", "<field_2>(<value_2>)", ...],
    where fields are sorted lexicographically.
    If the specified record does not exist, returns an empty list.
  • scan_by_prefix(self, key: str, prefix: str) -> list[str]
    — should return a list of strings representing some fields of a record associated with key.
    Specifically, only fields that start with prefix should be included.
    The returned list should be in the same format as in the scan operation with fields sorted in lexicographical order.

Examples

Queries Explanations
set("A", "BC", "E") database state: {“A”: {“BC”:“E”}}
set("A", "BD", "F") database state: {“A”: {“BC”:“E”,“BD”:“F”}}
set("A", "C", "G") database state: {“A”: {“BC”:“E”,“BD”:“F”,“C”:“G”}}
scan_by_prefix("A", "B") returns [“BC(E)”,“BD(F)”]
scan("A") returns [“BC(E)”,“BD(F)”,“C(G)”]
scan_by_prefix("B", "B") returns []

Level 3

Support the timeline of operations and TTL (Time-To-Live) settings for records and fields. Each operation from previous levels now has an alternative version with a timestamp parameter to represent when the operation was executed.
For each field - value pair in the database, the TTL determines how long that value will persist before being removed.

Notes

  • Time should always flow forward, so timestamps are guaranteed to strictly increase as operations are executed.
  • Each test cannot contain both versions of operations (with and without timestamp). However, you should maintain backward compatibility, so all previously defined methods should work in the same way as before.

Function Definitions

  • set_at(self, key: str, field: str, value: str, timestamp: int) -> None
  • set_at_with_ttl(self, key: str, field: str, value: str, timestamp: int, ttl: int) -> None
  • delete_at(self, key: str, field: str, timestamp: int) -> bool
  • get_at(self, key: str, field: str, timestamp: int) -> str | None
  • scan_at(self, key: str, timestamp: int) -> list[str]
  • scan_by_prefix_at(self, key: str, prefix: str, timestamp: int) -> list[str]

Examples

Example 1:

Queries Explanations
set_at_with_ttl("A", "BC", "E", 1, 9) database state: {“A”: {“BC”:“E”}} where“BC”:“E”expires at timestamp 10
set_at_with_ttl("A", "BC", "E", 5, 10) database state: {“A”: {“BC”:“E”}} updated expiry at timestamp 15
set_at("A", "BD", "F", 5) database state: {“A”: {“BC”:“E”,“BD”:“F”}}
scan_by_prefix_at("A", "B", 14) returns [“BC(E)”,“BD(F)”]
scan_by_prefix_at("A", "B", 15) returns [“BD(F)”]

Example 2:

Queries Explanations
set_at("A", "B", "C", 1) database state: {“A”: {“B”:“C”}}
set_at_with_ttl("X", "Y", "Z", 2, 15) database state: {“X”: {“Y”:“Z”}} expires at timestamp 17
get_at("X", "Y", 3) returns“Z”
set_at_with_ttl("A", "D", "E", 4, 10) database state: {“X”: {“Y”:“Z”},“A”: {“D”:“E”,“B”:“C”}}
scan_at("A", 13) returns [“B(C)”,“D(E)”]
scan_at("X", 16) returns [“Y(Z)”]
scan_at("X", 17) returns [] (expired)
delete_at("X", "Y", 20) returns False; the record“X”was expired at timestamp 17

Level 4

The database should be backed up from time to time. Introduce operations to support backing up and restoring the database state based on timestamps.
When restoring, ttl expiration times should be recalculated accordingly.

Function Definitions

  • backup(self, timestamp: int) -> int
    — should save the database state at the specified timestamp, including the remaining ttl for all records and fields.
    Returns the number of non-expired records in the database.
  • restore(self, timestamp: int, timestamp_to_restore: int) -> None
    — should restore the database from the latest backup before or at timestamp_to_restore.
    Expiration times for restored records and fields should be recalculated according to the timestamp of this operation.

Examples

Queries Explanations
set_at_with_ttl("A", "B", "C", 1, 10)
backup(3) returns 1
set_at("A", "D", "E", 4)
backup(5) returns 2
delete_at("A", "B", 8)
backup(9) returns 1
restore(10, 7) restores the database to backup at timestamp 5
backup(11) returns 2
scan_at("A", 15) returns [“B(C)”,“D(E)”]
scan_at("A", 16) returns [“D(E)”]
Coinbase OA: In-Memory DB – Full Solution & Examples
Coinbase OA: In-Memory DB – Full Solution & Examples

The VOprep team has long accompanied candidates through various major company OAs and VOs, including Coinbase, Google, Amazon, Citadel, SIG, providing real-time voice assistance, remote practice, and interview pacing reminders to help you stay smooth during critical moments. If you are preparing for these companies, you can check out our customized support plans—from coding interviews to system design, we offer full guidance to help you succeed.

END
 0