KatInterview

Mini Query Engine: Filter, Sort, and Project Rows

Data Structure DesignAuto-graded · 17 test cases
MediumAuto-gradedData Structure Designdata structuressortingfiltering

Last updated

Tests ability to implement a pipeline of filter, multi-column sort, and column projection over a list of in-memory row dicts.

Mini Query Engine: Filter, Sort, and Project Rows

You are given a table represented as a list of rows, where each row is a dictionary mapping column names to values. Implement a function that processes the table through three stages in order:

  1. Filter — keep only the rows that satisfy every condition in a given list.
  2. Sort — reorder the surviving rows by one or more columns, in either ascending or descending direction.
  3. Project — return only the requested columns from each surviving row.

Stage details

Filtering

Each condition is a three-element list [column, operator, value]. A row passes only when all conditions hold (AND semantics). Supported operators are =, !=, <, <=, >, >=. If a row is missing the column named in a condition, the row does not pass. An unrecognised operator must raise ValueError.

Sorting

order_by is a two-element list [sort_columns, ascending], where:

  • sort_columns is a list of column names defining the sort priority (first element = primary key, and so on).
  • ascending is a single boolean: True means smallest first, False means largest first, applied uniformly to every sort column.

Use a sequence of stable sorts moving from the least-significant key to the most-significant key. This produces correct lexicographic ordering across all sort columns while preserving the original row order for ties.

Projection

Build the output by keeping only the keys listed in columns, and only when the key is actually present in a surviving row. Omit keys that are absent from a particular row rather than inserting None.

Constraints

  • 0 <= len(rows) <= 10^4
  • 1 <= len(columns) <= 20
  • 0 <= len(conditions) <= 10
  • sort_columns contains between 1 and 5 column names; ascending is a single bool
  • Row values are comparable scalars (strings, numbers). ISO-format date strings such as "1906-12-09" sort correctly as plain strings.
  • Column names are non-empty strings.
  • All rows in rows share the same set of keys (no sparse schema).
  • order_by always contains exactly two elements; conditions may be an empty list.

Examples

Example 1

Input: run_query([{'id': '1', 'name': 'Ada', 'birthday': '1815-12-10'}, {'id': '2', 'name': 'Charles', 'birthday': '1791-12-26'}, {'id': '3', 'name': 'Grace', 'birthday': '1906-12-09'}], ['id'], [['name', '=', 'Charles']], [['birthday'], False])

Output: [{'id': '2'}]

Example 2

Input: run_query([{'id': '1', 'name': 'Ada', 'birthday': '1815-12-10'}, {'id': '2', 'name': 'Charles', 'birthday': '1791-12-26'}, {'id': '3', 'name': 'Grace', 'birthday': '1906-12-09'}], ['id', 'name'], [], [['birthday'], False])

Output: [{'id': '3', 'name': 'Grace'}, {'id': '1', 'name': 'Ada'}, {'id': '2', 'name': 'Charles'}]

Hints

Hint 1

Work through the pipeline in order: filter first, then sort, then project. Projecting before sorting is a common mistake — you may discard a column that you still need as a sort key.

Hint 2

For multi-column sorting, apply a stable sort once per key, moving from the least significant key to the most significant. Python's built-in sort is stable, so earlier passes are preserved when later passes encounter ties.

Hint 3

Build a lookup table mapping operator strings like '=' and '>=' to their corresponding Python functions (operator.eq, operator.gt, etc.) so each condition can be evaluated with a single dictionary lookup rather than a chain of if-statements.