Snowflake 面试题:实现一个迷你编程语言解释器(SnowCal Interpreter)

80次阅读
没有评论

SnowCal is a simple programming language used to perform calculations. A SnowCal program keeps a single integer X in memory, initially set to zero, and repeatedly performs addition and multiplication operations on it. Formally, the language has 5 operations:

  • ADD Y → add Y to X
  • MUL Y → multiply X by Y
  • FUN F → begin the definition of a function with unique name F
  • END → end of current function
  • INV F → invoke function F

Write an interpreter that takes in the commands as an array of strings and returns the final value of X.


Example Programs

Input:

MUL 2
ADD 3

Output:

3

Input:

FUN INCREMENT
  ADD 1
END

INV INCREMENT
MUL 2
ADD 3

这道题要求你实现一个简单编程语言 SnowCal 的解释器。语言中只有一个整数变量 X(初始为 0),支持五种指令:

  • ADD YX += Y
  • MUL YX *= Y
  • FUN F → 开始定义名为 F 的函数
  • END → 函数定义结束
  • INV F → 调用函数 F

你需要按顺序解析指令、保存函数定义,并在执行过程中正确处理函数调用(包括嵌套和多次调用)。最终返回执行完所有命令之后 X 的值。

这道面试题重点考察:

  • 解释器设计能力
  • 函数定义与作用域管理
  • 指令解析(parsing)
  • 状态管理与执行引擎实现

属于中高级工程师常见的系统设计类 Coding 题。

VOprep 团队长期陪同学员实战各类大厂 OA 与 VO,包括 SnowFlake、Google、Amazon、Citadel、SIG 等,提供实时答案助攻、远程陪练与面试节奏提醒,帮助大家在关键时刻不卡壳。
如果你也在准备公司,可以了解一下我们的定制助攻方案——从编程面到系统设计,全程护航上岸。

正文完
 0