Interview prompt
Explain a bytecode vm executes a portable instruction set to an engineer who understands the surrounding system but has not used this technique. Walk from its contract to a concrete operation, then discuss where it fails or becomes expensive.
A strong answer
Bytecode is a compact instruction representation designed for a virtual machine rather than one physical processor. A VM fetches instructions, decodes operands, and updates execution state such as a stack, registers, frames, and heap. This layer can provide portability and runtime checks.
A stack-based instruction sequence might push two constants, add the top values, and return the result. The VM’s instruction definitions specify operand widths, stack effects, and control-flow behavior. A verifier can reject malformed bytecode before execution if it proves stack and type constraints.
A complete answer also calls out the assumptions that control correctness. Interpretation has dispatch overhead, while compiled native code may execute more directly but requires architecture-specific generation. Bytecode is only portable when runtimes agree on format and semantics. Sandboxing a VM also requires controlling native calls, memory, and resource consumption.
Close by describing one representative test or measurement. For a stack VM, state the stack before and after an ADD instruction that consumes two numeric values. What invariant should a bytecode verifier enforce before this instruction executes?
Follow-up questions
Answer the follow-ups in the frontmatter. Use the linked article for the concept and the trace to make the explanation concrete.