FastPrepParse Variable, Expression, and Application Trees

Parse Variable, Expression, and Application Trees

SambaNova Systems logoSambaNova Systems● MediumFULLTIMENEW GRADPHONE SCREENONSITE INTERVIEW
Learn

Problem statement

Parse one valid nested expression into a canonical tree string.

  • An identifier becomes Variable(name).
  • Comma-separated children in parentheses become Expression(child1,...).
  • Comma-separated children in square brackets become Application(child1,...).

Whitespace may surround tokens and commas. Each group has at least one child. Return the canonical representation without spaces.

Function

parseExpressionTree(text: String) → String

Examples

Example 1

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

The nested parentheses produce nested Expression nodes.

Example 2

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

Square brackets create an Application node.

Example 3

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

A lone identifier is a Variable.

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 problems

See SambaNova Systems hiring insights
public String parseExpressionTree(String text) {
    // write your code here
}
text"(x,(x,y))"
expected"Expression(Variable(x),Expression(Variable(x),Variable(y)))"
Checking account…