Design an In-Memory Database
🗄️ What is an in-memory database? It is a small database that keeps all of its data in memory instead of on disk. You create tables with typed columns, insert rows, and ask for the rows that match some conditions, much like a tiny version of SQL. Indexes on chosen columns make those lookups fast.
This problem shows up in interviews in a few shapes: "build a mini SQL engine", "design a table store with simple queries", "add indexes to a database". They are the same design. The core is a set of tables, a way to describe a query (conditions, sort order, columns to return) and indexes that answer part of a query without looking at every row. If your interviewer gives you SQL text instead of method calls, that is a parser in front of the same design; we cover it in Extensibility.
It is a hard problem not because any one piece is clever, but because the pieces have to agree. A query that uses an index must return exactly what a full scan would return, and every insert, update and delete must keep every index correct. Most bugs in this problem live in that gap.
Requirements
"Design an in-memory database. Users can create tables, insert rows and run simple queries with conditions. Support indexes to make queries faster."
The prompt leaves almost everything open. The questions below are the ones that change the classes or the tests.
Clarifying Questions
You: What types can a column have?
Interviewer: Keep it to two: integers and strings. Values come in as strings, so the database has to check that an integer column really gets an integer.
You: Do we parse SQL text, or is the query a method call?
Interviewer: Method calls. Something like select(table, columns, where, orderBy). If there is time, we can talk about SQL text later.
You: What can a condition look like? Can it use OR, joins, or functions?
Interviewer: A condition is a column, an operator and a value. The operators are =, !=, <, <=, > and >=. A query can have several conditions, and a row must match all of them. No OR, no joins, no functions.
You: In what order do rows come back?
Interviewer: In the order they were inserted, unless the query asks to sort by one column. Sorting is ascending, and rows with equal values keep insertion order.
You: Who decides which columns are indexed?
Interviewer: The user, with an explicit createIndex call. An index must never change any result. It only makes queries faster.
You: What happens on bad input, like an unknown column or "abc" for an integer?
Interviewer: The call fails with a clear sentinel and changes nothing. No half-done updates.
You: Does it need to survive a restart, or handle many threads?
Interviewer: Memory only and single-threaded for now. We may come back to threads.
The fifth answer is the one to remember. "An index never changes a result" is a promise you can test: run the same queries before and after creating an index, and the answers must be the same. The practice tests do exactly that.
💡 Interview tip: Pin down result order early. "Insertion order unless sorted, ties keep insertion order" sounds like a detail, but without it two correct implementations return the same rows in different orders and you cannot even compare them. Real databases make no order promise without
ORDER BY; in an interview, a fixed rule is easier to reason about.
Final Requirements
- A database holds named tables. Creating a table that already exists fails.
- Each table has named columns, each of type
INTorSTRING. Values are passed as strings; anINTvalue must be an optional-and 1 to 9 digits. - Insert adds one row with a value for every column. A wrong count or an invalid value rejects the whole row.
- Select returns chosen columns (or all) of the rows that match every condition. A condition is
column op valuewith one of six comparison operators.INTvalues compare as numbers,STRINGvalues by character code. - Rows come back in insertion order, or sorted ascending by one column, with ties in insertion order.
- Update sets one column to a new value in every matching row and returns how many rows matched. Delete removes every matching row and returns how many were removed.
- A user can create an index on any column. Indexes never change a result, and they stay correct after inserts, updates and deletes.
- Unknown tables or columns, invalid values and invalid conditions fail with a sentinel and change nothing.
Out of scope
- Joins, OR, nested conditions, aggregates (
COUNT,SUM) andGROUP BY. - Parsing SQL text (Extensibility 1).
- Primary keys and unique constraints (Extensibility 3).
- Transactions, persistence and concurrent access (Extensibility 4).