FastPrepParse Variable, Expression, and Application Trees
Problems/Parse Variable, Expression, and Application Trees

Parse Variable, Expression, and Application Trees

SambaNova Systems logoSambaNova Systems● MediumFULLTIMENEW GRADPHONE SCREENONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: A firsthand SambaNova phone report dated March 28, 2022 asks candidates to parse strings such as (x,(x,y)) into the provided Variable, Expression, and Application classes. It notes that brackets mean application. Our version is about 93% the same as the reported question.

You are given text, a string that holds one valid nested expression. Turn it into a tree and return the tree written as a string.

  • An identifier becomes Variable(name).
  • Children inside parentheses, separated by commas, become Expression(child1,...).
  • Children inside square brackets, separated by commas, become Application(child1,...).

For example, (x,y) becomes Expression(Variable(x),Variable(y)).

Whitespace may appear around tokens and commas. Each group has at least one child. Return the tree string with no spaces in it.

Function

parseExpressionTree(text: String) → String

Examples

Example 1

text = "(x,(x,y))"return = "Expression(Variable(x),Expression(Variable(x),Variable(y)))"

The outer parentheses hold two children: x and (x,y). The first child becomes Variable(x). The inner parentheses become another Expression that holds Variable(x) and Variable(y). Putting them together gives the nested string shown.

Example 2

text = "[f,(x,y)]"return = "Application(Variable(f),Expression(Variable(x),Variable(y)))"

The square brackets make an Application node. Its first child f becomes Variable(f). Its second child (x,y) becomes an Expression that holds Variable(x) and Variable(y). That gives the output shown.

Example 3

text = "value_1"return = "Variable(value_1)"

The input is a single identifier with no brackets around it. So the whole answer is one Variable node that holds that name.

Constraints

  • 1 <= text.length <= 10000.
  • Identifiers contain ASCII letters, digits, and underscore and begin with a letter.
  • The input is syntactically valid, has one root, and nesting depth is at most 500.

More SambaNova Systems Onsite Interview questions

  • Vectorized K-Means AssignmentsONSITE INTERVIEW · Seen Apr 2024
  • 4Sum II Tuple CountONSITE INTERVIEW · Seen Jun 2022
  • Bomb Enemy Best CoordinatesONSITE INTERVIEW · Seen Jun 2022
  • Sum Nodes with an Even-Valued GrandparentONSITE INTERVIEW · Seen Jun 2022
  • Validate Binary Search TreeONSITE INTERVIEW · Seen Jun 2022
  • Deterministic Zigzag SortPHONE SCREEN · ONSITE INTERVIEW · Seen Jun 2022
  • Maximum Area of IslandONSITE INTERVIEW · Seen Jun 2022
  • Task Scheduler with CooldownPHONE SCREEN · ONSITE INTERVIEW · Seen Jun 2022
  • Point Inside a TrianglePHONE SCREEN · ONSITE INTERVIEW · Seen Jun 2022
  • Flip Equivalent Binary TreesONSITE INTERVIEW · Seen Oct 2021

Similar questions at other companies

  • Single-Pass S-Expression ParserBaseten · ONSITE INTERVIEWBaseten interview questions
  • Remove Parentheses from a Plus-Minus ExpressionOracle · PHONE SCREENOracle interview questions
  • Most Frequent Call Path From Function Trace LogsRoblox · PHONE SCREENRoblox interview questions
  • Decode StringCompass International Holdings · PHONE SCREENCompass International Holdings interview questions
  • Flatten a Mixed JSON ListNike, Inc. · ONSITE INTERVIEWNike, Inc. interview questions
  • Molecular Weight of a Grouped FormulaAgoda · ONSITE INTERVIEWAgoda interview questions
  • Calculate a Token-List ExpressionCollege Vine · ONSITE INTERVIEW
  • Rich Text Parser Part 3 (Depth Tokens)Figma · PHONE SCREEN · ONSITE INTERVIEWFigma interview questions
  • Parse Nested HTML into a TreeApple · ONSITE INTERVIEWApple interview questions
  • Calculator with ParenthesesGoogle · PHONE SCREEN · ONSITE INTERVIEWGoogle interview questions
All SambaNova Systems interview questionsSee SambaNova Systems hiring insights
Saved
class Solution {
    public String parseExpressionTree(String text) {
        // write your code here
    }
}
Checking account…