Design an In-Memory Database With Auto-Increment Row IDs and Cell Lookup

Quick Overview

Design an in-memory database of named tables that inserts rows with auto-incrementing IDs, deletes rows without ever reusing their IDs, and reads a single cell by row ID and 1-indexed column. It tests choosing hash-based structures, keeping per-table state, and handling deleted or missing rows.

Design an In-Memory Database With Auto-Increment Row IDs and Cell Lookup

Role: Software Engineer

Category: Software Engineering Fundamentals

Difficulty: easy

Interview Round: Other

You are given `n` tables, each identified by a unique name and having a predefined number of columns. Design an in-memory SQL-like database that supports inserting rows, deleting rows, and selecting individual cell values. Each table maintains an auto-incrementing row ID starting from `1`. When a row is deleted, its ID is not reused by future insertions. The row ID counter always increases. You are given two arrays `names` and `columns` of length `n`, where `names[i]` is the name of the `i`-th table and `columns[i]` is the number of columns in the `i`-th table. Implement the following methods: - `insertRow(name: str, row: List[str])`: Insert a row into the table `name`. The row id is auto-incremented starting from 1 for each table. - `deleteRow(name: str, rowId: int)`: Remove the row with id `rowId` from the table `name`. Future insertions will still use the next auto-incremented id (deleted ids are not reused). - `selectCell(name: str, rowId: int, columnIndex: int) -> str`: Return the value of the cell in the row with id `rowId` and the column index `columnIndex` (1-indexed) from the table `name`. Example 1: ```text Input: ["InMemoryDB", "insertRow", "selectCell", "insertRow", "deleteRow", "selectCell"] [[["one", "two", "three"], [2, 3, 1]], ["two", ["first", "second", "third"]], ["two", 1, 3], ["two", ["fourth", "fifth", "sixth"]], ["two", 1], ["two", 2, 2]] Output: [null, null, "third", null, null, "fifth"] Explanation: db = InMemoryDB(["one","two","three"], [2,3,1]) # 3 tables created db.insertRow("two", ["first","second","third"]) # table "two" row 1: [first, second, third] db.selectCell("two", 1, 3) # -> "third" (row 1, column 3) db.insertRow("two", ["fourth","fifth","sixth"]) # table "two" row 2: [fourth, fifth, sixth] db.deleteRow("two", 1) # deletes row 1 from table "two" db.selectCell("two", 2, 2) # -> "fifth" (row 2, column 2) ``` Example 2: ```text Input: ["InMemoryDB", "insertRow", "insertRow", "deleteRow", "insertRow", "selectCell"] [[["alpha"], [3]], ["alpha", ["a", "b", "c"]], ["alpha", ["d", "e", "f"]], ["alpha", 1], ["alpha", ["g", "h", "i"]], ["alpha", 3, 2]] Output: [null, null, null, null, null, "h"] Explanation: db = InMemoryDB(["alpha"], [3]) # 1 table with 3 columns db.insertRow("alpha", ["a","b","c"]) # row 1: [a, b, c] db.insertRow("alpha", ["d","e","f"]) # row 2: [d, e, f] db.deleteRow("alpha", 1) # deletes row 1 db.insertRow("alpha", ["g","h","i"]) # row 3: [g, h, i] (id 3, not reusing 1) db.selectCell("alpha", 3, 2) # -> "h" (row 3, column 2) ``` ```hint Reaching a row Every call identifies a row by its table name and row id. Pick structures that reach a row from those two keys directly, and think about which per-table state has to survive a delete. ``` ### Constraints & Assumptions - `1 <= n <= 10⁴` - `names.length == columns.length == n` - `1 <= names[i].length, value.length <= 20` - `names[i]` and `value` consist of lowercase English letters. - `1 <= columns[i] <= 10` - `1 <= columnIndex <=` the number of columns in the specified table - All `names[i]` are distinct. - At most `2 * 10⁴` calls will be made in total to `insertRow` and `deleteRow`. - For `selectCell`, `rowId` refers to a row that has been inserted and not deleted. ### Clarifying Questions to Ask - What should `deleteRow` do when the row id was never inserted or has already been deleted? - Should `insertRow` reject a row whose length differs from the table's column count? - Will every call name an existing table, or should an unknown name be reported? ### What a Strong Answer Covers - A representation that reaches any row from its table name and row id in constant time, with the trade-off against a list indexed by row id. - A per-table id counter that only increases, so a deleted id is never handed out again. - The conversion of the 1-indexed column argument and a defined behavior for deleting a row that does not exist. - The time and space cost of each operation. - Working code that reproduces both examples. ### Follow-up Questions - How would you support a query that returns every row id in a table whose given column equals a value, and keep it fast as rows are inserted and deleted? - What would change if several threads called these methods at the same time? - How would you make the data survive a process restart without ever reusing a deleted row id?

Overview: Design an in-memory database of named tables that inserts rows with auto-incrementing IDs, deletes rows without ever reusing their IDs, and reads a single cell by row ID and 1-indexed column. It tests choosing hash-based structures, keeping per-table state, and handling deleted or missing rows.

PracHub logo
General
Nov 24, 2026
easySoftware EngineerOtherSoftware Engineering Fundamentals
0
0

You are given n tables, each identified by a unique name and having a predefined number of columns. Design an in-memory SQL-like database that supports inserting rows, deleting rows, and selecting individual cell values.

Each table maintains an auto-incrementing row ID starting from 1. When a row is deleted, its ID is not reused by future insertions. The row ID counter always increases.

You are given two arrays names and columns of length n, where names[i] is the name of the i-th table and columns[i] is the number of columns in the i-th table.

Implement the following methods:

  • insertRow(name: str, row: List[str]) : Insert a row into the table name . The row id is auto-incremented starting from 1 for each table.
  • deleteRow(name: str, rowId: int) : Remove the row with id rowId from the table name . Future insertions will still use the next auto-incremented id (deleted ids are not reused).
  • selectCell(name: str, rowId: int, columnIndex: int) -> str : Return the value of the cell in the row with id rowId and the column index columnIndex (1-indexed) from the table name .

Example 1:

Input:
["InMemoryDB",                         "insertRow",                           "selectCell",  "insertRow",                           "deleteRow", "selectCell"]
[[["one", "two", "three"], [2, 3, 1]], ["two", ["first", "second", "third"]], ["two", 1, 3], ["two", ["fourth", "fifth", "sixth"]], ["two", 1],  ["two", 2, 2]]

Output: [null, null, "third", null, null, "fifth"]

Explanation:
db = InMemoryDB(["one","two","three"], [2,3,1])    # 3 tables created
db.insertRow("two", ["first","second","third"])    # table "two" row 1: [first, second, third]
db.selectCell("two", 1, 3)                         # -> "third"  (row 1, column 3)
db.insertRow("two", ["fourth","fifth","sixth"])    # table "two" row 2: [fourth, fifth, sixth]
db.deleteRow("two", 1)                             # deletes row 1 from table "two"
db.selectCell("two", 2, 2)                         # -> "fifth"  (row 2, column 2)

Example 2:

Input:
["InMemoryDB",     "insertRow",                "insertRow",                "deleteRow",  "insertRow",                "selectCell"]
[[["alpha"], [3]], ["alpha", ["a", "b", "c"]], ["alpha", ["d", "e", "f"]], ["alpha", 1], ["alpha", ["g", "h", "i"]], ["alpha", 3, 2]]

Output: [null, null, null, null, null, "h"]

Explanation:
db = InMemoryDB(["alpha"], [3])         # 1 table with 3 columns
db.insertRow("alpha", ["a","b","c"])    # row 1: [a, b, c]
db.insertRow("alpha", ["d","e","f"])    # row 2: [d, e, f]
db.deleteRow("alpha", 1)                # deletes row 1
db.insertRow("alpha", ["g","h","i"])    # row 3: [g, h, i] (id 3, not reusing 1)
db.selectCell("alpha", 3, 2)            # -> "h"  (row 3, column 2)

Constraints & Assumptions

  • 1 <= n <= 10⁴
  • names.length == columns.length == n
  • 1 <= names[i].length, value.length <= 20
  • names[i] and value consist of lowercase English letters.
  • 1 <= columns[i] <= 10
  • 1 <= columnIndex <= the number of columns in the specified table
  • All names[i] are distinct.
  • At most 2 * 10⁴ calls will be made in total to insertRow and deleteRow .
  • For selectCell , rowId refers to a row that has been inserted and not deleted.

Clarifying Questions to Ask Guidance

  • What should deleteRow do when the row id was never inserted or has already been deleted?
  • Should insertRow reject a row whose length differs from the table's column count?
  • Will every call name an existing table, or should an unknown name be reported?

What a Strong Answer Covers Guidance

  • A representation that reaches any row from its table name and row id in constant time, with the trade-off against a list indexed by row id.
  • A per-table id counter that only increases, so a deleted id is never handed out again.
  • The conversion of the 1-indexed column argument and a defined behavior for deleting a row that does not exist.
  • The time and space cost of each operation.
  • Working code that reproduces both examples.

Follow-up Questions Guidance

  • How would you support a query that returns every row id in a table whose given column equals a value, and keep it fast as rows are inserted and deleted?
  • What would change if several threads called these methods at the same time?
  • How would you make the data survive a process restart without ever reusing a deleted row id?