Design an in-memory column index with exact lookups and range queries

Quick Overview

Design an in-memory index for one table column that maps keys to row IDs and supports insert, delete, exact search and inclusive range queries with sorted output. It tests picking data structures for equality and range lookups, keeping results in order, and analyzing the cost of each operation.

Design an in-memory column index with exact lookups and range queries

Company: Snowflake

Role: Software Engineer

Category: Software Engineering Fundamentals

Difficulty: medium

Interview Round: Other

Design and implement a basic indexing structure for a specific column in a mock database table. The index maps column values (keys) to row IDs, similar to how a database index accelerates lookups. The index should support inserting and deleting key and row ID associations, performing exact key lookups, and executing range queries to find all row IDs whose keys fall within a given inclusive range. When returning row IDs, results must be sorted in ascending order. For range queries, results are ordered by key ascending first, then by row ID ascending within the same key. Implement the following methods: - `insert(key: int, rowId: int)`: insert a key and rowId pair into the index, associating the given row ID with the specified key. - `delete(key: int, rowId: int) -> bool`: delete a key and rowId pair from the index. Returns `True` if the pair was found and deleted, `False` otherwise. - `search(key: int) -> int[]`: return a sorted list of all row IDs associated with the given key. Returns an empty list if the key is not found. - `rangeQuery(startKey: int, endKey: int) -> int[]`: return all row IDs whose keys fall within the inclusive range `[startKey, endKey]`, ordered by key ascending, then by row ID ascending. Example 1: ```text Input: ["ColumnIndex", "insert", "insert", "insert", "insert", "rangeQuery", "delete", "rangeQuery", "search"] [[], [10, 7], [3, 42], [10, 2], [7, 5], [3, 10], [7, 5], [5, 10], [7]] Output: [null, null, null, null, null, [42, 5, 2, 7], true, [2, 7], []] idx = ColumnIndex() # initialize empty index idx.insert(10, 7) # index: {10: [7]} idx.insert(3, 42) # index: {3: [42], 10: [7]} idx.insert(10, 2) # index: {3: [42], 10: [2, 7]} idx.insert(7, 5) # index: {3: [42], 7: [5], 10: [2, 7]} idx.rangeQuery(3, 10) # -> [42, 5, 2, 7] (key 3, then key 7, then key 10) idx.delete(7, 5) # -> True (key 7 has no row IDs left) idx.rangeQuery(5, 10) # -> [2, 7] (only key 10 is left in the range) idx.search(7) # -> [] ``` The first range query returns row 42 before row 5 because results are grouped by key first. Row IDs are sorted only within a key. Example 2: ```text Input: ["ColumnIndex", "insert", "insert", "insert", "search", "delete", "delete", "search", "rangeQuery"] [[], [5, 100], [5, 200], [5, 150], [5], [5, 200], [5, 999], [5], [1, 10]] Output: [null, null, null, null, [100, 150, 200], true, false, [100, 150], [100, 150]] idx = ColumnIndex() # initialize empty index idx.insert(5, 100) # index: {5: [100]} idx.insert(5, 200) # index: {5: [100, 200]} idx.insert(5, 150) # index: {5: [100, 150, 200]} idx.search(5) # -> [100, 150, 200] (all row IDs sorted) idx.delete(5, 200) # -> True (removed rowId 200) idx.delete(5, 999) # -> False (rowId 999 not found for key 5) idx.search(5) # -> [100, 150] (remaining row IDs) idx.rangeQuery(1, 10) # -> [100, 150] (key 5 is in range [1, 10]) ``` ```hint Two kinds of lookup Exact lookups and range scans want different things from a structure. Decide which part of your design has to keep keys in order, and how a range query can find its first key without scanning every key. ``` ```hint Watch the deletes Think about what should happen to a key once its last row ID is deleted, and how leftover entries would affect later range queries. ``` ### Constraints & Assumptions - `1 <= key <= 10^6` - `1 <= rowId <= 10^6` - At most `10^4` calls total to `insert`, `delete`, `search`, and `rangeQuery`. - A `(key, rowId)` pair will not be inserted more than once without being deleted first. ### Clarifying Questions to Ask - What should `rangeQuery` return when `startKey` is greater than `endKey`? - Is the workload mostly reads or mostly writes, and could the index grow well beyond these limits? - Does the index only need to live in memory, or should the design carry over to pages on disk? - May `search` and `rangeQuery` return views of internal data, or must callers get copies? ### What a Strong Answer Covers - A structure that serves both exact lookups and ordered range scans, with the reason for each part. - How results stay in the required order: row IDs kept sorted per key, keys visited in ascending order. - Delete semantics: the `True`/`False` result, and removing a key once its last row ID is gone. - Time and space complexity per operation, including the cost of keeping arrays sorted on insert and delete. - Edge cases such as empty ranges, `startKey > endKey`, missing keys and returning copies, plus how a balanced tree or B+ tree would change the costs. ### Follow-up Questions - How would you make `insert` and `delete` O(log n) in the worst case, and which structure would you use in Python, Java or C++? - How does a disk-based database implement this kind of index, and why are the leaf pages of a B+ tree linked together? - How would you support a composite index on two columns, and which queries could use it? - What changes if the table has hundreds of millions of rows and the index no longer fits in memory?

Overview: Design an in-memory index for one table column that maps keys to row IDs and supports insert, delete, exact search and inclusive range queries with sorted output. It tests picking data structures for equality and range lookups, keeping results in order, and analyzing the cost of each operation.

Snowflake logo
Snowflake
Nov 24, 2026
mediumSoftware EngineerOtherSoftware Engineering Fundamentals
0
0

Design and implement a basic indexing structure for a specific column in a mock database table. The index maps column values (keys) to row IDs, similar to how a database index accelerates lookups.

The index should support inserting and deleting key and row ID associations, performing exact key lookups, and executing range queries to find all row IDs whose keys fall within a given inclusive range.

When returning row IDs, results must be sorted in ascending order. For range queries, results are ordered by key ascending first, then by row ID ascending within the same key.

Implement the following methods:

  • insert(key: int, rowId: int) : insert a key and rowId pair into the index, associating the given row ID with the specified key.
  • delete(key: int, rowId: int) -> bool : delete a key and rowId pair from the index. Returns True if the pair was found and deleted, False otherwise.
  • search(key: int) -> int[] : return a sorted list of all row IDs associated with the given key. Returns an empty list if the key is not found.
  • rangeQuery(startKey: int, endKey: int) -> int[] : return all row IDs whose keys fall within the inclusive range [startKey, endKey] , ordered by key ascending, then by row ID ascending.

Example 1:

Input:
["ColumnIndex", "insert", "insert", "insert", "insert", "rangeQuery", "delete", "rangeQuery", "search"]
[[],            [10, 7],  [3, 42],  [10, 2],  [7, 5],   [3, 10],      [7, 5],   [5, 10],      [7]]

Output: [null, null, null, null, null, [42, 5, 2, 7], true, [2, 7], []]

idx = ColumnIndex()       # initialize empty index
idx.insert(10, 7)         # index: {10: [7]}
idx.insert(3, 42)         # index: {3: [42], 10: [7]}
idx.insert(10, 2)         # index: {3: [42], 10: [2, 7]}
idx.insert(7, 5)          # index: {3: [42], 7: [5], 10: [2, 7]}
idx.rangeQuery(3, 10)     # -> [42, 5, 2, 7]  (key 3, then key 7, then key 10)
idx.delete(7, 5)          # -> True  (key 7 has no row IDs left)
idx.rangeQuery(5, 10)     # -> [2, 7]  (only key 10 is left in the range)
idx.search(7)             # -> []

The first range query returns row 42 before row 5 because results are grouped by key first. Row IDs are sorted only within a key.

Example 2:

Input:
["ColumnIndex", "insert", "insert", "insert", "search", "delete", "delete", "search", "rangeQuery"]
[[],            [5, 100], [5, 200], [5, 150], [5],      [5, 200], [5, 999], [5],      [1, 10]]

Output: [null, null, null, null, [100, 150, 200], true, false, [100, 150], [100, 150]]

idx = ColumnIndex()       # initialize empty index
idx.insert(5, 100)        # index: {5: [100]}
idx.insert(5, 200)        # index: {5: [100, 200]}
idx.insert(5, 150)        # index: {5: [100, 150, 200]}
idx.search(5)             # -> [100, 150, 200]  (all row IDs sorted)
idx.delete(5, 200)        # -> True   (removed rowId 200)
idx.delete(5, 999)        # -> False  (rowId 999 not found for key 5)
idx.search(5)             # -> [100, 150]  (remaining row IDs)
idx.rangeQuery(1, 10)     # -> [100, 150]  (key 5 is in range [1, 10])

Constraints & Assumptions

  • 1 <= key <= 10^6
  • 1 <= rowId <= 10^6
  • At most 10^4 calls total to insert , delete , search , and rangeQuery .
  • A (key, rowId) pair will not be inserted more than once without being deleted first.

Clarifying Questions to Ask Guidance

  • What should rangeQuery return when startKey is greater than endKey ?
  • Is the workload mostly reads or mostly writes, and could the index grow well beyond these limits?
  • Does the index only need to live in memory, or should the design carry over to pages on disk?
  • May search and rangeQuery return views of internal data, or must callers get copies?

What a Strong Answer Covers Guidance

  • A structure that serves both exact lookups and ordered range scans, with the reason for each part.
  • How results stay in the required order: row IDs kept sorted per key, keys visited in ascending order.
  • Delete semantics: the True / False result, and removing a key once its last row ID is gone.
  • Time and space complexity per operation, including the cost of keeping arrays sorted on insert and delete.
  • Edge cases such as empty ranges, startKey > endKey , missing keys and returning copies, plus how a balanced tree or B+ tree would change the costs.

Follow-up Questions Guidance

  • How would you make insert and delete O(log n) in the worst case, and which structure would you use in Python, Java or C++?
  • How does a disk-based database implement this kind of index, and why are the leaf pages of a B+ tree linked together?
  • How would you support a composite index on two columns, and which queries could use it?
  • What changes if the table has hundreds of millions of rows and the index no longer fits in memory?