Implement an in-memory SQL-like table
OpenAISoftware Engineer · OnsiteLast updated
This question evaluates implementation skills for an in-memory SQL-like table, focusing on data modeling with nested key-value structures, string handling, efficient lookups, and lexicographic sorting with tie-breaking.
Problem
Implement a simple in-memory database for one table. All values are strings.
Each row is identified by a rowKey (string). Each row contains columns identified by colKey (string).
You must process a sequence of commands and output results for read/query commands.
Supported commands
SET rowKey colKey value
- Set
table[rowKey][colKey] = value.
GET rowKey colKey
- Output the value if present, otherwise output
NULL.
SELECT whereCol whereValue orderByCol
- Return all rowKeys for rows where
table[rowKey][whereCol] == whereValue. - Sort the matching rows by the value of
orderByColin ascending lexicographic order. - If a row is missing
orderByCol, treat its sort value as an empty string"". - If multiple rows have the same
orderByColvalue, break ties byrowKeyascending. - Output the resulting
rowKeys joined by a single space (or output an empty line if none).
Input/Output format
- Input: list of commands (one per line).
- Output:
- For each
GET, print a single line. - For each
SELECT, print a single line.
Constraints (assume)
- Up to (2 \times 10^5) commands.
- Total length of all strings is within reasonable memory limits.
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.