KatInterview

In-Memory Row Store with Filter, Sort, and Projection

Data Structure DesignAuto-graded · 3 test cases
HardAuto-gradedData Structure Designdesignsimulationsorting

Last updated

Tests ability to build a stateful table abstraction that processes insert and query operations with filtering, sorting, and column projection.

Problem

You are building a lightweight in-memory row store. Given a fixed list of column names at construction time, your store must handle two types of operations applied in sequence: inserting rows and querying them.

Implement the function run_table(columns, operations, arguments) that processes a series of operations on a single table and returns their results in order.

Parameters

  • columns: A list of strings naming the columns of the table, in schema order.
  • operations: A list of strings, each either "insert" or "query".
  • arguments: A list of argument lists, one per operation (same length and order as operations).

Operation Specs

"insert"

arguments[i] is a one-element list containing a dict that maps column names to values. Store the row. Values for columns not present in the dict should be recorded as None. Return None for this operation.

"query"

arguments[i] is a three-element list [select, where, order_by]:

  • select: A list of column names to include in each returned row. If None, include all columns in schema order.
  • where: A dict of equality conditions — only rows where every specified column matches the given value are included. If None, all rows pass. A condition on a column that does not exist in the schema matches no rows (return []).
  • order_by: A two-element list [column_name, direction] where direction is "asc" or "desc". If None, rows are returned in insertion order.

Return the list of matching row dicts (projected to the selected columns).

Sorting Rules

  • When two rows have equal values in the sort column, their relative insertion order is preserved (stable sort).
  • None values sort before all non-None values in "asc" order and after all non-None values in "desc" order.
  • When a column contains a mix of integers and strings, integers sort before strings.

Return Value

Return a list with one entry per operation:

  • None for each "insert".
  • A list of dicts for each "query".

Constraints

  • 1 ≤ len(columns) ≤ 50
  • All column names are non-empty strings.
  • 1 ≤ len(operations) ≤ 1000
  • Row values are integers, strings, or None.
  • select columns, where keys, and order_by columns are drawn from the schema (except where edge-case rules above apply).
  • At most 500 "insert" operations and at most 500 "query" operations.

Examples

Example 1

Input: run_table(['name', 'age', 'city'], ['insert', 'insert', 'insert', 'query'], [[{'name': 'Alice', 'age': 30, 'city': 'NY'}], [{'name': 'Bob', 'age': 25, 'city': 'LA'}], [{'name': 'Charlie', 'age': 30, 'city': 'SF'}], [['name'], {'age': 30}, ['name', 'desc']]])

Output: [None, None, None, [{'name': 'Charlie'}, {'name': 'Alice'}]]

Hints

Hint 1

Process each operation in sequence and accumulate results. For inserts, normalize the incoming dict so every schema column is always present (using None for missing ones). This keeps query logic uniform.

Hint 2

For the where filter, iterate over all stored rows and check every condition in the where dict. If the where dict references a column not in the schema, no row can ever match, so return [] immediately.

Hint 3

Sort after filtering but project (select) after sorting. That way you can sort by a column that isn't in the select list. Use a stable sort with a composite key that encodes type first (None, int, str), then value, then flip direction with reverse=True.