PPractick

DSA interview question

Postfix to Infix Conversion

Easy · Data structures · Topics: stack, string

The problem

A debugger for a stack-based virtual machine shows the bytecode's expressions in postfix, like ab*c+, but developers want to read them as ordinary fully parenthesised infix, like ((a*b)+c). Postfix is already in evaluation order, so the conversion mirrors evaluation. Read left to right, push operands, and when an operator appears, pop the right operand (top) and the left operand, then push "(" + left + op + right + ")".

Example: ab*c+ → ((a*b)+c).

Function signature

string postfixToInfix(const string& post);  // single-character operands; binary operators

Test cases

InputOutputExplanation
ab*c+((a*b)+c)
ABC/-AK/L-*((A-(B/C))*((A/K)-L))
ab+(a+b)
abc++(a+(b+c))The grouping comes from the postfix order
aa

(Confirm: the postfix expression is valid; every binary operation is wrapped in parentheses.)

How interviewers phrase it

Clarifying questions to ask

  1. Fully parenthesised output? (Yes.)
  2. Only binary operators and single-character operands?
  3. Is the input guaranteed valid?

Follow-ups to expect

  1. "Which operand is popped first?"
  2. "Why parenthesise everything?"
  3. "Can you output minimal parentheses?"
  4. "How is this related to evaluation?"
  5. "Complexity?"

Practise questions like this with a real engineer

On Practick another engineer interviews you live for 60 minutes on a question from this bank. They have the full interviewer guide beside the call: the expected answer to every follow-up, hints to give if you stall, and a scoring rubric. You get the model solution once the session settles. It is free: interview someone for an hour and you have earned your own.

Book a free mock interview

Related questions

All 474 dsa interview questions

DSA mock interviews · System design interview questions · Free mock interview practice · Browse interviewers · Sign in