Dataford
Interview QuestionsInterview GuidesExperiencesCoaching
New
Pricing
Get started
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
Experiences
Practice
Mock InterviewsTimed interview simulations with feedbackCoaching1:1 coaching with people at your target companiesSuccess 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
Build my plan
RPN-like Calculator VM
00:00
5 left

RPN-like Calculator VM

HardPython

Problem

Process a string containing numbers and operators: + - * /, plus "swap", "dup" (duplicate), "pop", "drop". (Full operator spec hidden; visible test cases include ('-5 3 + .', [-2]), ('1 . 2 . 3 . 4', [1, 2, 3]), ('99 2 3 + .', [5]), ('1 2 drop .', [1]), ('20 5 / .', [4]), ('30 3 / 4 - .', [6]).)

Asked in the tech phone screen 1 stage. Hand-written Python, no AI suggestions allowed; must handle negative number tokens like -5.

Implement evaluate(program). Tokens are separated by whitespace. Numbers are pushed onto a stack. Arithmetic operators pop the right operand first, then the left operand, and push the result. swap exchanges the top two values, dup copies the top value, pop removes the top value, drop is an alias for removing the top value, and . removes the top value and appends it to the output. Division truncates toward zero. Return the output list in evaluation order. Inputs are valid and never underflow the stack.

Constraints

  • 1 <= number of tokens <= 10^4
  • Each numeric token represents a signed 32-bit integer
  • Tokens are separated by one or more whitespace characters
  • The program is valid and never performs an operation on an insufficient stack
  • Division operands are nonzero
  • Division truncates toward zero

Function Signature

def evaluate(program):
Interviewer

Your question is RPN-like Calculator VM. 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.