Parse and Evaluate Filtered Aggregations Over Transaction Rows

Last updated: November 29, 2026

Quick Overview

Write a small parser and evaluator for filter expressions such as `amount > 100 and (status == "ok" or country != "US")`, supporting comparisons, and, or, not and parentheses with correct precedence, then use it to compute count, sum or mean of a column over the rows that match.

Chalk
Coding & Algorithms
Software Engineer
Chalk
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Hard

0

0

0 solved


Write a small parser and evaluator for filter expressions such as `amount > 100 and (status == "ok" or country != "US")`, supporting comparisons, and, or, not and parentheses with correct precedence, then use it to compute count, sum or mean of a column over the rows that match.

Chalk lets users write feature expressions that filter and aggregate related rows, such as summing a user's transactions above a threshold, and the engine must parse them once and evaluate them fast. The interviewer is looking for a clean tokenizer, a recursive descent parser that respects precedence, null semantics, and parsing once rather than per row.

What the Interviewer Expects
  • Separates tokenizing, parsing into an AST, and evaluating
  • Encodes precedence: not binds tighter than and, which binds tighter than or
  • Raises precise syntax errors for unbalanced parentheses and stray tokens
  • Defines null semantics: comparisons with a missing value are false
  • Parses the expression once and evaluates the tree per row
  • Handles mean over zero rows without dividing by zero
Key Topics to Cover
recursive descent parsing
tokenization
operator precedence
AST evaluation
null semantics
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • How would you compile the AST into vectorized column operations (masks over arrays) instead of per-row evaluation?
  • How would you type-check the expression against the feature schema before running it?
  • How would you add `in [..]` and arithmetic like `amount * fx_rate > 100`?
  • SQL uses three-valued logic for NULL. Where does the simple 'false' rule give a different answer?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements

Literals: integers, floats, double-quoted strings, true and false. Identifiers are column names. Operators: == != > < >= <=, and, or, not, parentheses. Output: count, sum or mean of `col...

High-Level Design

Grammar, lowest precedence first:

  • or := and ('or' and)*
  • and := not ('and' not)*
  • not := 'not' not | cmp
  • cmp := atom (OP atom)?
  • atom := literal | column | '(' or ')'

A regex tokeniz...


Submit Your Answer
Markdown supported

Related Questions