Parse and Evaluate a Small Alpha Expression Language
Last updated: November 29, 2026
Quick Overview
Write a parser and evaluator for expressions such as rank(ts_delta(close, 5)) / delay(volume, 1), supporting numbers, data fields, + - * /, unary minus, parentheses and a set of named operators, evaluated over a dates by instruments matrix.
Worldquant
November 29, 20260
0
0 solved
Write a parser and evaluator for expressions such as rank(ts_delta(close, 5)) / delay(volume, 1), supporting numbers, data fields, + - * /, unary minus, parentheses and a set of named operators, evaluated over a dates by instruments matrix.
WorldQuant BRAIN lets users write alphas in Fast Expression, a compact operator language evaluated over market data panels. A platform engineer interview may ask for a stripped down version to test tokenizing, precedence, a clean AST, vectorized evaluation, and sensible errors, all in under an hour.
What the Interviewer Expects
- Writes a tokenizer and a recursive descent parser with correct precedence and left associativity
- Represents the AST as hashable tuples or small classes
- Evaluates over numpy arrays with NaN-safe operations (division by zero to NaN)
- Raises clear errors for unknown fields, operators and syntax
- Caches identical subtrees so repeated subexpressions run once
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- How would you type-check the window argument so ts_mean(close, volume) is rejected before running?
- How would you share cached subexpressions across many different user alphas?
- How would you prevent a user expression from consuming unbounded memory or CPU?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Grammar: expr := term (('+'|'-') term), term := unary ((''|'/') unary)*, unary := '-' unary | atom, atom := number | field | name '(' args ')' | '(' expr ')'. Fields are float arrays of shape (dates...
High-Level Design
Regex tokenizer, recursive descent parser producing nested tuples like ('call', 'rank', (args...)), and an evaluator dispatching to numpy. Tuples are hashable, so a dict keyed by subtree caches common...