← Google Interview Insights

Google·Software Engineer·Onsite - Coding / Algorithms·Intermediate

IntermediatePrefer not to say
Jul 2026

Summary

Google SWE coding round, one problem the whole session. Converting RPN to infix with minimal parentheses. Sounds manageable until you actually sit down and think through all the precedence and associativity edge cases.

Questions Asked (1)

Q1

Given a Reverse Polish Notation expression as a comma-separated string, convert it to an equivalent infix expression using the minimum number of parentheses necessary to preserve correctness.

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

The stack-based structure clicked pretty fast for me.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Use a stack to parse the RPN expression, building expression trees for operands and operators. Then perform an inorder traversal of the tree, adding parentheses only when the child operator has lower precedence than the parent, or when associativity requires it (e.g., right child with same precedence for left-associative operators).

Pro tip: Clarify the operator precedence and associativity rules upfront, as they determine where parentheses are needed. Also, consider edge cases like unary minus or invalid expressions to show thoroughness.

1. Parse the RPN expression

Split the input string by commas and iterate through tokens. Use a stack to build an expression tree: push operands as leaf nodes; for each operator, pop two nodes, create a new node with the operator and the two nodes as children, and push it back.

2. Define precedence and associativity

Establish a precedence map for operators (e.g., +,-: 1; *,/: 2) and note that most binary operators are left-associative. This will guide parentheses insertion during traversal.

3. Generate infix with minimal parentheses

Perform an inorder traversal of the expression tree. For each operator node, recursively generate the left and right subexpressions. Add parentheses around a child if its operator has lower precedence than the parent, or if it has equal precedence but is the right child of a left-associative operator.

4. Handle edge cases and validate

Consider unary operators (e.g., negative numbers) and ensure the output is a valid infix expression. Optionally, verify by converting back to RPN or evaluating both expressions to confirm equivalence.

Key Points to Mention

  • Stack-based parsing of RPN to build an expression tree
  • Operator precedence and associativity rules
  • Parentheses insertion rules: lower precedence child, or equal precedence right child for left-associative operators
  • Inorder traversal for infix generation
  • Handling unary operators and negative numbers
  • Time and space complexity: O(n) for parsing and traversal

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.