In-Memory Database Implementation Task
Your task is to implement a simplified version of an in-memory database. All operations that should be supported by this database are described below.
Solving this task consists of several levels. Subsequent levels are opened when the current level is correctly solved. You always have access to the data for the current and all previous levels.
You are not required to provide the most efficient implementation. Any code that passes the unit tests is sufficient.
You can execute a single test case by running the following command in the terminal:
bash run_single_test.sh "<test_case_name>"
Requirements
Your task is to implement a simplified version of an in-memory database. Plan your design according to the level specifications below.
Level 1
In-memory database should support basic operations to manipulate records, fields, and values within fields.
Level 2
In-memory database should support displaying a record’s fields based on a filter.
Level 3
In-memory database should support the TTL (Time-To-Live) settings for records.
To move to the next level, you need to pass all the tests at this level when submitting the solution.
Level 1 Function Details
The basic level of the in-memory database contains records. Each record can be accessed with a unique identifier key, which is of string type. A record contains several field-value pairs, with field as string type and value as integer type. All operations have a timestamp parameter — a stringified timestamp in milliseconds. It is guaranteed that all timestamps are unique and are in a range from 1 to 10⁹. Operations will be given in order of strictly increasing timestamps. Timestamps will be needed starting from Level 3.
set(timestamp, key, field, value)
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, a new one is created.
compare_and_set(timestamp, key, field, expected_value, new_value)
Should update the value of field
in the record associated with key
to new_value
if the current value equals expected_value
.
- If
expected_value
does not match the current value, or eitherkey
orfield
does not exist, this operation is ignored. - This operation should return
True
if the field was updated andFalse
otherwise.
compare_and_delete(timestamp, key, field, expected_value)
Should remove the field field
in the record associated with key
if the previous value equals expected_value
.
- If
expected_value
does not match the current value, or if thekey
orfield
does not exist, this operation is ignored. - This operation should return
True
if the field was removed andFalse
otherwise.
get(timestamp, key, field)
Should return the value contained within field
of the record associated with key
.
- If the record or the field does not exist, should return
None
.
我们长期稳定承接各大科技公司如TikTok、Google、Amazon等的OA笔试代写服务,确保满分通过。如有需求,请随时联系我们。
We consistently provide professional online assessment services for major tech companies like TikTok, Google, and Amazon, guaranteeing perfect scores. Feel free to contact us if you're interested.
