Design an In-Memory Wide-Column Store with Equality Secondary Indexes
Quick Overview
Design an in-memory sparse wide-column store with equality secondary indexes that are backfilled on creation and kept exact through overwrites and row deletes. It tests inverted index maintenance, per-operation cost analysis and deterministic ASCII ordering of lookup results.
Design an In-Memory Wide-Column Store with Equality Secondary Indexes
Company: CRED
Role: Software Engineer
Category: Software Engineering Fundamentals
Difficulty: medium
Interview Round: Other
Design an in-memory sparse wide-column store with optional equality secondary indexes. Each row is identified by a string `rowId` and holds any number of string cells keyed by column name. Different rows may have entirely different columns.
A secondary index on a column maps each cell value to the set of rows holding that value. Creating an index backfills all existing cells for that column. After that, every write and row deletion must keep the index accurate: remove stale memberships and keep other rows that share the same value. A column can be indexed before any row uses it, and the index stays in place even after its last row is deleted.
Missing cells are represented by the empty string `""`, which is unambiguous because all real strings are non-empty. Indexed lookups must go through the index and must never scan every row, and results are returned in ASCII order.
Implement an `IndexedWideColumnStore` class with these methods:
- `put(rowId: str, column: str, value: str) -> None` creates the row if it is absent and inserts or overwrites the cell, keeping the row's other cells. It updates the column's index if one exists.
- `get(rowId: str, column: str) -> str` returns the cell value, or the empty string if the row or the cell is absent.
- `getRow(rowId: str) -> List[List[str]]` returns the row's `[column, value]` pairs sorted by column, or `[]` if the row is absent.
- `deleteRow(rowId: str) -> bool` deletes the row and removes it from every index it appears in. It returns `True` if the row existed and `False` otherwise.
- `createIndex(column: str) -> bool` creates an equality index on the column and backfills it from existing rows. It returns `True` when the index is created and `False` if the column is already indexed.
- `find(column: str, value: str) -> List[str]` returns the IDs of the rows whose cell in the indexed column equals `value`, sorted in ASCII order. It returns `[]` if the column is not indexed or nothing matches.
In the examples below, `put` returns nothing, so its lines show no result.
Example 1, backfill and an overwrite:
```text
put("u1", "city", "NYC")
put("u2", "city", "SF")
put("u3", "city", "NYC")
put("u1", "age", "30")
createIndex("city") -> True
createIndex("city") -> False
find("city", "NYC") -> ["u1", "u3"]
put("u1", "city", "SF")
find("city", "NYC") -> ["u3"]
find("city", "SF") -> ["u1", "u2"]
get("u1", "city") -> "SF"
getRow("u1") -> [["age", "30"], ["city", "SF"]]
find("age", "30") -> []
```
The index on "city" is created after three rows already hold that column, so the backfill puts "u1" and "u3" under "NYC" and "u2" under "SF". Overwriting "u1" with "SF" moves it out of the "NYC" set while "u3" stays there. "age" was never indexed, so `find` on it returns an empty list even though "u1" has that value.
Example 2, an index created before use, deletes and ASCII order:
```text
createIndex("tier") -> True
put("b", "tier", "gold")
put("B", "tier", "gold")
put("a_1", "tier", "gold")
put("_x", "tier", "gold")
put("z", "tier", "silver")
find("tier", "gold") -> ["B", "_x", "a_1", "b"]
deleteRow("B") -> True
deleteRow("B") -> False
find("tier", "gold") -> ["_x", "a_1", "b"]
get("B", "tier") -> ""
deleteRow("z") -> True
find("tier", "silver") -> []
createIndex("tier") -> False
put("y", "tier", "silver")
find("tier", "silver") -> ["y"]
```
In ASCII, uppercase letters come before the underscore, which comes before lowercase letters, so "B" sorts first and "_x" sorts before "a_1". Deleting "B" leaves the other "gold" rows in place. Deleting "z", the last "silver" row, empties that value, but the "tier" index still exists: a second `createIndex` fails and the later write of "y" is indexed.
```hint Overwrites
When `put` overwrites a cell in an indexed column, the row is still listed under its old value unless you remove it. Work out what `put` has to look at before it writes.
```
```hint Cost of a delete
Aim for a `deleteRow` whose cost depends only on the number of cells in that row. Ask which index entries a single row can possibly appear in.
```
### Constraints & Assumptions
- All `rowId`, `column` and `value` strings contain 1 to 40 ASCII letters, digits or underscores.
- Different rows may share the same cell value.
- At most 10,000 operations in total.
- At most 20,000 live cells at any time.
- `find` must use the secondary index and must not scan all rows.
### Clarifying Questions to Ask
- Does `getRow` sort column names in the same ASCII order that `find` uses for row IDs?
- Is there any way to delete a single cell, or do cells only disappear when their whole row is deleted?
- Is the store used from one thread, or must a reader never see a write half applied to the indexes?
### What a Strong Answer Covers
- The storage layout for sparse rows and the layout of each secondary index.
- How every write path (a new cell, an overwritten cell, a deleted row) keeps the indexes exact, including for other rows that share a value.
- The backfill in `createIndex`, plus an index that exists before any row uses its column and after its last row is gone.
- The time cost of each operation and the memory used, with `find` costing time in proportion to the number of matches.
- Deterministic ASCII ordering for the results of `find` and `getRow`.
### Follow-up Questions
- How would you add `deleteCell(rowId, column)`, and what should happen to a row that loses its last cell?
- How would you support range lookups, such as every row whose value in a column falls between two strings?
- `find` sorts its matches on every call. How would you avoid that cost when one value has thousands of rows and is queried often?
- How would you keep rows and indexes consistent when several threads write at once?
Overview: Design an in-memory sparse wide-column store with equality secondary indexes that are backfilled on creation and kept exact through overwrites and row deletes. It tests inverted index maintenance, per-operation cost analysis and deterministic ASCII ordering of lookup results.
Design an in-memory sparse wide-column store with optional equality secondary indexes. Each row is identified by a string rowId and holds any number of string cells keyed by column name. Different rows may have entirely different columns.
A secondary index on a column maps each cell value to the set of rows holding that value. Creating an index backfills all existing cells for that column. After that, every write and row deletion must keep the index accurate: remove stale memberships and keep other rows that share the same value. A column can be indexed before any row uses it, and the index stays in place even after its last row is deleted.
Missing cells are represented by the empty string "", which is unambiguous because all real strings are non-empty. Indexed lookups must go through the index and must never scan every row, and results are returned in ASCII order.
Implement an IndexedWideColumnStore class with these methods:
put(rowId: str, column: str, value: str) -> None
creates the row if it is absent and inserts or overwrites the cell, keeping the row's other cells. It updates the column's index if one exists.
get(rowId: str, column: str) -> str
returns the cell value, or the empty string if the row or the cell is absent.
getRow(rowId: str) -> List[List[str]]
returns the row's
[column, value]
pairs sorted by column, or
[]
if the row is absent.
deleteRow(rowId: str) -> bool
deletes the row and removes it from every index it appears in. It returns
True
if the row existed and
False
otherwise.
createIndex(column: str) -> bool
creates an equality index on the column and backfills it from existing rows. It returns
True
when the index is created and
False
if the column is already indexed.
find(column: str, value: str) -> List[str]
returns the IDs of the rows whose cell in the indexed column equals
value
, sorted in ASCII order. It returns
[]
if the column is not indexed or nothing matches.
In the examples below, put returns nothing, so its lines show no result.
The index on "city" is created after three rows already hold that column, so the backfill puts "u1" and "u3" under "NYC" and "u2" under "SF". Overwriting "u1" with "SF" moves it out of the "NYC" set while "u3" stays there. "age" was never indexed, so find on it returns an empty list even though "u1" has that value.
Example 2, an index created before use, deletes and ASCII order:
In ASCII, uppercase letters come before the underscore, which comes before lowercase letters, so "B" sorts first and "_x" sorts before "a_1". Deleting "B" leaves the other "gold" rows in place. Deleting "z", the last "silver" row, empties that value, but the "tier" index still exists: a second createIndex fails and the later write of "y" is indexed.
Constraints & Assumptions
All
rowId
,
column
and
value
strings contain 1 to 40 ASCII letters, digits or underscores.
Different rows may share the same cell value.
At most 10,000 operations in total.
At most 20,000 live cells at any time.
find
must use the secondary index and must not scan all rows.
Clarifying Questions to Ask Guidance
Does
getRow
sort column names in the same ASCII order that
find
uses for row IDs?
Is there any way to delete a single cell, or do cells only disappear when their whole row is deleted?
Is the store used from one thread, or must a reader never see a write half applied to the indexes?
What a Strong Answer Covers Guidance
The storage layout for sparse rows and the layout of each secondary index.
How every write path (a new cell, an overwritten cell, a deleted row) keeps the indexes exact, including for other rows that share a value.
The backfill in
createIndex
, plus an index that exists before any row uses its column and after its last row is gone.
The time cost of each operation and the memory used, with
find
costing time in proportion to the number of matches.
Deterministic ASCII ordering for the results of
find
and
getRow
.
Follow-up Questions Guidance
How would you add
deleteCell(rowId, column)
, and what should happen to a row that loses its last cell?
How would you support range lookups, such as every row whose value in a column falls between two strings?
find
sorts its matches on every call. How would you avoid that cost when one value has thousands of rows and is queried often?
How would you keep rows and indexes consistent when several threads write at once?