Math Interpreter with Function Definitions
Implement a math interpreter that processes a list of string commands acting on a single global integer x (initially 0). Commands are: ADD k (add k to x), MUL k (multiply x by k), FUN name (begin defining a named function), END (end the current function definition), and INV name (invoke a previously defined function on the current value of x). Functions do not execute when defined — only when invoked. A function body may contain ADD, MUL, and calls to other previously defined functions. There are no recursive calls or nested function definitions, and every invoked function is guaranteed to have been defined earlier. Commands execute sequentially, and functions may be invoked multiple times.
Asked at:
Snowflake
Question Timeline
See when this question was last asked and where, including any notes left by other candidates.
Late May, 2026
Snowflake
Junior
You are creating an in-memory store that gets a stream of operations and you are supposed to perform this on a single address. The operations will be in this format: "ADD 3" -> add 3 to the address, "MUL 4" -> multiply 4 to the address, "FUN ABC" -> you are going to get function definition for the function name ABC. This will be till you encounter the next "END". "INV ABC" -> invoke function ABC and perform all of its operations on the address space
Late February, 2026
Snowflake
Junior
Implement a simple math interpreter that processes a list of string commands acting on a single global integer x, initially 0. The commands may be ADD k (add k to x), MUL k (multiply x by k), FUN name (start defining a function named name), END (end the current function definition), and INV name (invoke a previously defined function on the current value of x). A function does not execute when defined; it only executes when invoked, and its body may contain ADD, MUL, and calls to other functions. There are no recursive calls or nested function definitions, and every invoked function is guaranteed to have been defined earlier. All commands are executed sequentially, and functions may be invoked many times.
Hello Interview Premium
Your account is free and you can post anonymously if you choose.