Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Browse
Browse All RolesEvery role hub, from analyst to MLBrowse All CompaniesCompany-specific interview loopsAll Interview GuidesThe full guide library
Top questions by role
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Top questions by skill
SQLPythonStatisticsMachine LearningA/B TestingSystem DesignGenerative AIProduct SenseMetricsBehavioral
Browse all questions →Try a mock interview
ExperiencesCoaching
New
Practice
Mock InterviewsTimed interview simulations with feedbackSuccess PathYour 6-week structured planModulesCurated lessons by topicWebinarsTalks from ex-Big Tech data leadsPlaygroundA free-form scratch editor
Learn
BlogInterview strategy and career adviceTech Job Market ReportHiring trends across data and AI rolesFor UniversitiesDataford for career centersAbout DatafordWho we are and how we build
Pricing
Get started
Evaluate Postfix With Bindings
00:00
5 left

Evaluate Postfix With Bindings

EasyPython

Problem

Tessian policy evaluation can represent arithmetic conditions in postfix notation so they can be processed efficiently. Given expression tokens and a mapping of variable names to integer values, evaluate the expression using the current bindings.

Tokens may be integer literals, variable names, or binary operators +, -, *, and /. Operators consume the two most recent values from the stack. For a / b, division must truncate toward zero. Every expression is valid, every referenced variable has a binding, and division by zero does not occur.

Formal Specification

Implement eval_postfix(tokens, bindings).

  • tokens is a list of strings in postfix order.
  • bindings is a dictionary mapping variable-name strings to integers.
  • Return the resulting integer.
  • Variable names contain lowercase English letters and underscores and do not begin with a digit.
  • A token consisting of an optional leading minus sign followed by digits is an integer literal.

Constraints

  • 1 <= len(tokens) <= 10^4
  • Each integer literal and binding value is between -10^9 and 10^9
  • The expression contains at least one value and has valid postfix syntax
  • At most len(tokens) distinct variable names are referenced
  • Division by zero does not occur

Function Signature

def eval_postfix(tokens, bindings):
Interviewer

Your question is Evaluate Postfix With Bindings. 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.
Code
Sign up free to run your codeLog inLn 2
Run your code to see test output here.