The stack-based conversion part I got pretty quickly.
Use a stack to evaluate the RPN expression while building an expression tree. Then perform an in-order 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 of '-' or '/').
Pro tip: Clarify the input format and operator precedence/associativity upfront, and mention that you'd handle edge cases like unary minus or invalid expressions. This shows attention to detail and real-world robustness.
Iterate through the RPN tokens. For each operand, push a leaf node; for each operator, pop two nodes, create an operator node with them as children, and push it back. The final stack top is the root.
Assign precedence levels (e.g., +,-:1; *,/:2) and note that all operators are left-associative. This determines when parentheses are needed.
Recursively traverse the tree in-order. For each operator node, decide whether to parenthesize its left and/or right child based on precedence and associativity rules.
Consider unary operators, division by zero, and invalid RPN. Optionally, verify by converting back to RPN or evaluating both expressions.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.