KatInterview

Build a Record Filter with Nested Query Conditions

Data Structure DesignAuto-graded · 20 test cases
HardAuto-gradedData Structure Designrecursiondata-structuresquery-evaluation

Last updated

Evaluate structured filter expressions with comparison, membership, and logical operators over in-memory records

Problem Statement

Build a system that stores dictionary records in memory and filters them using a structured query language. The language supports field comparisons, membership tests, and nested logical combinations.

Implement a function execute_record_operations that processes a sequence of operations on a record store:

  • "insert": Add a dictionary record to the store.
  • "query": Return all stored records matching a filter condition.

Filter Condition Format

A condition is represented as a dictionary and falls into one of two categories:

1. Field Comparison: { "field": { "operator": value } }

Available operators:

  • "$eq": Field equals the given value
  • "$gt": Field is strictly greater than the value
  • "$lt": Field is strictly less than the value
  • "$in": Field value appears in the provided list
  • "$nin": Field value does not appear in the provided list

2. Logical Combination:

  • { "$and": [cond1, cond2, ...] }: All conditions must evaluate to true
  • { "$or": [cond1, cond2, ...] }: At least one condition must evaluate to true
  • { "$not": cond }: Negates the result of the condition

An empty condition {} matches all records.

Field Evaluation Rules

  • Records are dictionaries with string keys and values that may be integers, floats, strings, or None.
  • If a field referenced in a condition does not exist in a record, treat it as None.
  • For "$gt" and "$lt":
    • If the field value is None, the comparison returns False.
    • If the field value and comparison target have incompatible types (e.g., integer vs string), the comparison returns False.
  • Records are returned in insertion order.

Function Signature

def execute_record_operations(operations: List[str], arguments: List[List[object]]) -> List[object]
  • operations[i] is either "insert" or "query"
  • For "insert", arguments[i][0] is the record dictionary
  • For "query", arguments[i][0] is the condition dictionary
  • Return a list where:
    • Insert operations contribute None
    • Query operations contribute a list of matching records

Constraints

  • At most 10,000 insert operations
  • At most 5,000 query operations
  • Maximum nesting depth of logical operators is 10
  • Field names contain only alphanumeric characters
  • String values contain only alphanumeric characters

Examples

Example 1

Input: execute_record_operations(['insert', 'insert', 'insert', 'query'], [[{'id': 1, 'name': 'Alice', 'age': 30, 'status': 'ACTIVE'}], [{'id': 2, 'name': 'Bob', 'age': 24, 'status': 'PENDING'}], [{'id': 3, 'name': 'Charlie', 'age': 35, 'status': 'ACTIVE'}], [{'$and': [{'status': {'$eq': 'ACTIVE'}}, {'age': {'$gt': 25}}]}]])

Output: [None, None, None, [{'id': 1, 'name': 'Alice', 'age': 30, 'status': 'ACTIVE'}, {'id': 3, 'name': 'Charlie', 'age': 35, 'status': 'ACTIVE'}]]

Example 2

Input: execute_record_operations(['insert', 'insert', 'insert', 'query'], [[{'id': 1, 'name': 'Alice', 'age': 30, 'status': 'ACTIVE'}], [{'id': 2, 'name': 'Bob', 'age': 24, 'status': 'PENDING'}], [{'id': 3, 'name': 'Charlie', 'age': 35, 'status': 'ACTIVE'}], [{'$or': [{'status': {'$in': ['PENDING', 'ARCHIVED']}}, {'name': {'$eq': 'Charlie'}}]}]])

Output: [None, None, None, [{'id': 2, 'name': 'Bob', 'age': 24, 'status': 'PENDING'}, {'id': 3, 'name': 'Charlie', 'age': 35, 'status': 'ACTIVE'}]]

Hints

Hint 1

Model the condition as a tree where leaves are field tests and internal nodes are logical operators, then evaluate it recursively for each record

Hint 2

Handle missing fields by using dict.get() which returns None by default, matching the specified behavior

Hint 3

For comparison operators that might fail with incompatible types, wrap them in try-except to return False on TypeError