Query an Indexed In-Memory Numeric Table
Retell AISoftware Engineer · Technical ScreenLast updated
This problem tests the ability to implement efficient multi-column indexing using binary search and sorted set intersections to handle complex range queries.
Problem Statement
You are tasked with building a lightweight indexing system for an in-memory table of records. Each record consists of a unique row ID (an integer) and a set of numeric columns. The table is immutable once created.
Implement a class IndexedTable with the following requirements:
__init__(self, data): The inputdatais a list of dictionaries, where each dict has anidkey and several numeric column keys (e.g.,{'id': 1, 'age': 25, 'score': 90}). Preprocess this data to support efficient range queries on any column.query(self, filters): Thefiltersargument is a dictionary where keys are column names and values are tuples(min_val, max_val)(inclusive). The method should return a sorted list ofids that satisfy all provided filters (logical AND).
Constraints
- The number of rows
Nis up to 100,000. - The number of columns is small (constant).
queryshould perform significantly faster than a full table scan.
Example
table = IndexedTable([
{'id': 1, 'age': 20, 'score': 80},
{'id': 2, 'age': 30, 'score': 90},
{'id': 3, 'age': 25, 'score': 70}
])
# Returns [2, 3] because both satisfy age >= 25 and score >= 70
table.query({'age': (25, 35), 'score': (70, 100)})
Edge Cases
- Empty filters (return all row IDs).
- Filters covering no records (return empty list).
- Columns requested that do not exist in the dataset.
Hints
Hint 1
For each column, store a list of (value, id) tuples sorted by the column value to enable binary search.
Hint 2
Use the `bisect` module to find the range of indices in your sorted column lists that satisfy the filter criteria.
Hint 3
After getting candidate IDs for each filter, use an intersection approach (like merging sorted lists or using sets) to find rows that satisfy all conditions.
Follow-up questions
- How would you optimize the intersection step if one filter is extremely restrictive while others are broad?
- If the table were mutable (rows added/deleted), how would your indexing strategy change?
- What are the space complexity trade-offs of creating indexes for every column versus only a subset?
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.