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.
def evaluate(program):