Evaluate Creator Bonus Formulas With Variables and Parentheses
Last updated: November 29, 2026
Quick Overview
Creator incentive rules are stored as integer arithmetic formulas over named metrics, such as `views / 1000 * 2 + (gifts - refunds)`. Write `evaluate(expr, variables)` supporting + - * /, unary minus, parentheses and integer division that truncates toward zero, raising clear errors for malformed input.
TikTok
November 29, 20260
0
0 solved
Creator incentive rules are stored as integer arithmetic formulas over named metrics, such as `views / 1000 * 2 + (gifts - refunds)`. Write `evaluate(expr, variables)` supporting + - * /, unary minus, parentheses and integer division that truncates toward zero, raising clear errors for malformed input.
TikTok onsite coding rounds often include one parsing problem with real product framing, here the Creator Rewards payout configuration. The interviewer wants a correct grammar with operator precedence and associativity, sane error handling for config typos, and code you could defend in a payments-adjacent code review.
What the Interviewer Expects
- Writes down the grammar (expression, term, factor) before coding
- Gets precedence, left associativity and unary minus right
- Truncating division handled explicitly, not via float conversion
- Reports malformed input and unknown variables instead of silently returning a number
- Explains why eval() is unacceptable for operator-authored config
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 add exponentiation, which is right-associative?
- Formulas are evaluated for millions of creators nightly; how would you avoid re-parsing each time?
- How would you support decimal money values without floating point error?
- How would you statically validate a formula when an operator saves it?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Integers and identifiers, binary + - * /, unary +/-, parentheses, whitespace anywhere. / truncates toward zero (so -7/2 = -3), matching what most payout specs mean. Errors: illegal character...
High-Level Design
Tokenize first, then run a recursive descent parser over the grammar:
- expression := term (('+'|'-') term)*
- term := factor ((''|'/') factor)
- factor := ('+'|'-') factor | number | name | '(' ex...