Implement an in-memory SQL-like table
Company: OpenAI
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: hard
Interview Round: Onsite
Overview: 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.
Read the full OpenAI Software Engineer interview experience this question came from
Constraints
- 0 <= len(commands) <= 2 * 10^5
- rowKey, colKey, whereCol, whereValue, orderByCol, and value are strings without spaces
- All comparisons and sorting are lexicographic string operations
- A SET command overwrites the previous value of the same cell, if any
Examples
Input: ["SET r1 name bob", "SET r1 age 2", "SET r2 name bob", "SET r2 age 10", "GET r1 name", "SELECT name bob age"]
Expected Output: ["bob", "r2 r1"]
Explanation: GET returns the stored value "bob". For SELECT, both rows match name=bob, and they are sorted by age as strings: "10" comes before "2" lexicographically, so r2 appears before r1.
Input: ["GET missing name", "SELECT status active score"]
Expected Output: ["NULL", ""]
Explanation: The table is empty. GET returns NULL, and SELECT has no matching rows so it returns an empty string.
Hints
- Use a nested dictionary for the actual table: rowKey -> {colKey: value}.
- To avoid scanning every row for SELECT, keep an index from (colKey, value) to the set of rowKeys that currently match that exact value.