Run a Stack Language Interpreter
Updated
Problem statement
Execute a simple stack program from instruction index 0 with an empty stack. Return the sequence of strings produced by PRINT, in execution order.
PUSH valuepushes the entire string after the first space, including an empty string.POPremoves the top string; it does nothing on an empty stack.PRINTappends the top string to the output without removing it; on an empty stack it appends"".TRUNCATE kremoves up tokcharacters from the right end of the top string. It leaves an empty string whenkis at least its length, and does nothing on an empty stack.JUMP targetcontinues at the zero-based indextarget.JUMP_IF_EMPTY targetjumps when the stack is empty or its top string is empty; otherwise execution advances normally.
Every instruction other than a taken jump advances to the next index. Reaching index instructions.length ends execution. Inputs are valid and guaranteed to terminate; detecting infinite loops is unnecessary.
Function
runInterpreter(instructions: String[]) → String[]Examples
Example 1
instructions = ["PUSH hello","PRINT","TRUNCATE 2","PRINT"]return = ["hello","hel"]Printing preserves the top value. Truncation then removes its last two characters.
Example 2
instructions = ["PUSH ","JUMP_IF_EMPTY 4","PUSH skip","PRINT","PUSH ok","PRINT"]return = ["ok"]The empty top value takes the jump directly to index 4, skipping the intervening instructions.
Example 3
instructions = ["POP","PRINT","PUSH abc","JUMP 5","PRINT"]return = [""]Popping an empty stack is harmless. The first print emits an empty string, and the final jump exits the program.
Constraints
- For this exercise, assume
0 <= instructions.length <= 1000and at most10000executed instructions. - A pushed value has at most
50printable ASCII characters, excluding line breaks. Its spaces are significant. - Non-
PUSHoperands are unsigned decimal integers. Truncation counts are at most50; jump targets are in[0, instructions.length]. - Opcodes are exactly the uppercase names above. Operand instructions have one separating space;
PUSHrepresents an empty value.POPandPRINThave no operand.
More Jane Street Phone Screen questions
- Maximum-Scoring Consecutive Tree LevelsPHONE SCREEN · Seen Nov 2026
- Tree Selection and VisibilityPHONE SCREEN · Seen Nov 2026
- Load/Store Arithmetic InterpreterPHONE SCREEN · Seen Oct 2026
- Instrument Stack Language JumpsPHONE SCREEN · Seen Oct 2026
- Positive-Integer Expression EvaluationPHONE SCREEN · Seen Oct 2026
- Add Two Integers Given as StringsPHONE SCREEN · Seen Sep 2026
- Angle Between Clock HandsPHONE SCREEN · Seen Jul 2026
- Route Length From Two CyclistsPHONE SCREEN · Seen Jul 2026
- Sort TradesPHONE SCREEN · ONSITE INTERVIEW · Seen Jul 2026
- Painted Cube: Count and RollPHONE SCREEN · Seen Jul 2026
Similar questions at other companies
- Adjacent Pair and Run RemovalWhatNot · PHONE SCREEN · ONSITE INTERVIEWWhatNot interview questions
- Compress Editor Actions into Operational Transform OperationsReplit · OA · PHONE SCREEN · ONSITE INTERVIEW
- Multiline Text Editor with Cursor MovementDatologyAI · PHONE SCREEN
- Trace Nested Object Destruction OrderArista Networks · PHONE SCREENArista Networks interview questions
- Stack Language EvaluatorRetell AI · PHONE SCREENRetell AI interview questions
- Text Document HistoryNotion · PHONE SCREEN · ONSITE INTERVIEWNotion interview questions
- Text Editor, Part 2: Undo and RedoOpenAI · PHONE SCREENOpenAI interview questions
- One-Dimensional Candy CrushBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
- Trace Events from Stable Whole-Stack StatesAnthropic · ONSITE INTERVIEWAnthropic interview questions
- Most Frequent Call Path in Interleaved LogsRoblox · ONSITE INTERVIEWRoblox interview questions
Jane Street interview experiences
- Jane Street TDO UK and Hong Kong: 22 Experiences (Part 10 of 10)
- Jane Street TDOE US Final Round: 11 Experiences (Part 5 of 10)
- Jane Street TDO Phone Math Round: 26 Experiences (Part 9 of 10)
- Jane Street TDOE UK Phone Screen: 15 Experiences (Part 4 of 10)
- Jane Street TDO Excel and VBA Test: 23 Experiences (Part 8 of 10)
- Jane Street TDOE UK Coding Round: 15 Experiences (Part 3 of 10)