Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
Evaluate a Basic Arithmetic Expression
00:00
5 left

Evaluate a Basic Arithmetic Expression

MediumPython

Problem

Basic calculator: evaluate an expression with + - * /.

Asked in the VO coding stage. From SWE question bank. Reported follow-up: Add support for parentheses.

Contract

Implement def calculate(expression):. The input is a valid string containing non-negative integers, spaces, and the operators +, -, *, and /. Return the integer result. Apply standard precedence, and truncate division toward zero. Parentheses are not included.

Constraints

  • 1 <= len(expression) <= 1000
  • The expression contains at least one integer
  • The expression contains only digits, spaces, and +, -, *, /
  • The expression is valid
  • Division by zero does not occur

Function Signature

def calculate(expression):
Interviewer

Your question is Evaluate a Basic Arithmetic Expression. Start with the requirements in the Question tab.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.
CodePython 3
You need to log in / sign up to run or submit.Ln 2
Run your code to see test output here.