Evaluate Alpha Formula Strings With Variables and Precedence
Last updated: November 29, 2026
Quick Overview
Implement an evaluator for formula strings such as `(close - open) / vol` that supports numbers, named variables, unary minus, parentheses and the four arithmetic operators with standard precedence.
Voleon
November 29, 20260
0
0 solved
Implement an evaluator for formula strings such as `(close - open) / vol` that supports numbers, named variables, unary minus, parentheses and the four arithmetic operators with standard precedence.
Research platforms at quant firms like Voleon often let researchers define signals as small expressions over data fields, so parsing them correctly matters. The interviewer is checking tokenization, a real parsing technique (recursive descent or precedence climbing), left associativity, and helpful error messages rather than `eval`.
What the Interviewer Expects
- Refuse to use Python eval and explain why
- Tokenize numbers, identifiers, operators and parentheses
- Implement precedence climbing or recursive descent
- Get left associativity right for minus and divide
- Handle unary minus and nested parentheses
- Raise clear errors for unknown variables and malformed input
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 extend this to function calls like `ts_mean(close, 20)`?
- How would you compile the expression once and evaluate it over whole arrays?
- How would you add exponentiation, which is right associative?
- How would you detect lookahead, such as a formula referencing tomorrow's close?
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: numbers (with optional decimals), identifiers bound in an env dict, + - * /, unary -, and parentheses. * and / bind tighter than + and -; all binary operators are left associati...
High-Level Design
eval would execute arbitrary code from a researcher's config, so we parse ourselves. A regex tokenizer produces (kind, value) tuples. Precedence climbing parses: parse_expr(min_prec) reads a una...