KatInterview

Match Documents Against Filter Expressions

Trees & GraphsAuto-graded · 20 test cases
MediumAuto-gradedTrees & Graphsrecursiontreesdictionaries

Last updated

Build a recursive evaluator for filter expressions with logical operators and field comparisons, testing tree traversal and nested field lookup.

You're building a component that determines whether data records satisfy filter criteria. Filter criteria are represented as nested expression trees, and you need to evaluate them against candidate records.

Write a function that accepts a filter expression and a data record, returning whether the record satisfies the filter.

Filter Expression Structure

A filter expression is a dictionary that can be either a logical operator node or a comparison node.

Logical Operator Nodes

  • {"$and": [expr_1, expr_2, ...]} — Returns True when every sub-expression returns True. An empty list returns True.
  • {"$or": [expr_1, expr_2, ...]} — Returns True when at least one sub-expression returns True. An empty list returns False.
  • {"$not": expr} — Returns the opposite of the sub-expression's result.

Comparison Nodes

  • {"field": str, "op": str, "val": any}

The field specifies which record field to examine. Nested fields use dot notation: "user.profile.email" accesses record["user"]["profile"]["email"].

The op specifies the comparison:

  • "$eq" — field value equals val
  • "$ne" — field value does not equal val
  • "$gt" — field value is greater than val
  • "$gte" — field value is greater than or equal to val
  • "$lt" — field value is less than val
  • "$lte" — field value is less than or equal to val
  • "$in" — field value appears in val (where val is a list)

Missing Fields

When a field path doesn't exist in the record:

  • All comparison operations return False, except
  • "$ne" returns True

A path is considered missing if any intermediate step fails (e.g., accessing "a.b" when "a" contains a non-dictionary value, or when a key doesn't exist).

Constraints

  • Expression tree depth ≤ 100
  • Field paths contain only alphanumeric characters and dots
  • Comparison operations $gt, $gte, $lt, $lte are only applied to comparable types
  • All expressions are well-formed according to the specification

Examples

Example 1

Input: match_filter({'$and': [{'field': 'age', 'op': '$gte', 'val': 18}, {'field': 'status', 'op': '$eq', 'val': 'active'}]}, {'name': 'Alice', 'age': 22, 'status': 'active'})

Output: True

Example 2

Input: match_filter({'$or': [{'field': 'user.role', 'op': '$in', 'val': ['admin', 'moderator']}, {'$not': {'field': 'user.banned', 'op': '$eq', 'val': True}}]}, {'user': {'role': 'guest', 'banned': False}})

Output: True

Hints

Hint 1

Use a sentinel object to distinguish missing fields from fields with None values

Hint 2

Handle logical operators recursively, letting Python's all() and any() manage empty lists and short-circuiting

Hint 3

Walk field paths segment by segment, checking that each intermediate value is a dictionary