Reported September 2024
Odoorecursion

Evaluate a Python Integer Expression

Reported by candidates from Odoo's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live Odoo OA. Under 2s to a working solution.
Founder's read

Odoo reported this one in September 2024, and the mistake that sinks a first attempt is treating it like a basic calculator. It's an expression parser with Python semantics: floor division, sign-matching modulo, right-associative **, and a unary minus that binds looser than ** on its left. If you're taking this OA soon, plan for a recursive descent parser, not a stack hack. Get the precedence ladder right and the code is short. Get it wrong and examples 2 and 3 fail. StealthCoder sits invisibly as a safety net if you blank on the grammar during the live OA.

The problem

Given a string expression, evaluate it and return its integer value.
Supported expression language
Nonnegative decimal integer literals.
Parentheses and ASCII space characters.
Unary + and -.
Binary +, -, *, //, %, and **.
Variables, function calls, strings, collections, and all other Python syntax are outside this exercise.
Evaluation rules
Parenthesized expressions are evaluated first.
** is right-associative. It binds more tightly than a unary sign on its left, so -2 ** 2 means -(2 ** 2).
Unary + and - are applied next.
*, //, and % share the next precedence level.
Binary + and - have the lowest precedence.
All supported binary operators other than ** are left-associative.
Division and modulo use Python integer semantics. The quotient a // b rounds down toward negative infinity. The remainder satisfies a == (a // b) * b + (a % b) and is either zero or has the same sign as b.

Function
evaluateExpression(expression: String) → long

Examples
Example 1
expression = "2 + 3 * 4"
return = 14
Multiplication has higher precedence than addition, so the value is 2 + 12 = 14.
Example 2
expression = "-2 ** 2 + 17 // 5"
return = -1
Exponentiation is evaluated before the unary minus, giving -(2 ** 2) = -4. Also, 17 // 5 = 3, so the result is -4 + 3 = -1.
Example 3
expression = "(-7) % 3 + 2 ** 3 ** 2"
return = 514
Python modulo gives (-7) % 3 = 2. Exponentiation is right-associative, so 2 ** 3 ** 2 = 2 ** 9 = 512. The total is 514.
Example 4
expression = "18 // -5 + 18 % -5"
return = -6
Python floor division gives 18 // -5 = -4, and the matching remainder is 18 % -5 = -2. Their sum is -6.

Constraints
1 <= expression.length <= 2000.
expression is syntactically valid under the supported grammar.
Every divisor is nonzero.
Every exponent is a nonnegative integer.
Every integer literal, intermediate arithmetic result, and the final result fits in a signed 64-bit integer.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick is a precedence-climbing or recursive descent parser with one function per level: add/sub, then mul/floordiv/mod, then unary, then power, then atom. The key detail is that power sits below unary in the call chain, so -2 ** 2 parses as -(2 ** 2). Power's right side calls unary again, so 2 ** -1 style input and chains like 2 ** 3 ** 2 resolve right-associatively. The common pitfall is language arithmetic. In Java or C++, / and % truncate toward zero, so 18 // -5 gives -3 instead of -4. You must write floor division and modulo by hand, adjusting when the remainder is nonzero and signs differ. Also compute ** with fast exponentiation, and tokenize multi-digit numbers while skipping spaces. Values fit in 64 bits, so long is enough. If the grammar slips away mid-assessment, StealthCoder is the hedge that hands you the structure while you type.

Drill it cold or hedge it with StealthCoder. Either way, don't walk into the OA hoping you remember the trick.

If this hits your live OA

You can drill Evaluate a Python Integer Expression cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Made for the candidate who got the OA invite this morning and has 72 hours, not six months.

Get StealthCoder

Related leaked OAs

⏵ The honest play

You've seen the question. Make sure you actually pass Odoo's OA.

Odoo reuses patterns across OAs. Made for the candidate who got the OA invite this morning and has 72 hours, not six months. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Evaluate a Python Integer Expression FAQ

What's the trick in the Odoo expression evaluation problem?+

Write a recursive descent parser with one function per precedence level. Put power below unary so -2 ** 2 equals -4. Make power right-associative by recursing on the right side. Everything else is careful tokenizing and Python-style floor math.

Why do examples 2 and 4 fail in most first attempts?+

Two reasons. Applying unary minus before the exponent gives 4 instead of -4, and using truncating division gives -3 instead of -4 for 18 // -5. Fix the grammar order and implement floor division and modulo manually.

Can I just use eval or a language built-in?+

Don't count on it. The problem asks you to implement evaluation, and many languages lack Python semantics for // and %. Even where eval exists, it's likely disallowed or unavailable. Write the parser yourself so behavior matches the spec.

How do I implement Python floor division in Java or C++?+

Compute q = a / b and r = a % b using the native operators. If r is nonzero and the signs of r and b differ, subtract 1 from q and add b to r. Then a == q * b + r holds with Python semantics.

How do I prepare for this in 48 hours?+

Write the parser from scratch twice, using the four examples as tests. Practice the precedence chain: add, mul, unary, power, atom. Then test edge cases like nested parentheses, 2 ** 3 ** 2, negative divisors, and long chains of spaces and signs.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with Odoo.

OA at Odoo?
Invisible during screen share
Get it