DSA interview question
A spreadsheet's formula engine converts formulas like a+b*(c^d-e) into postfix (Reverse Polish Notation), where operators follow their operands, so they can be evaluated with a simple stack and no precedence rules. The conversion is Dijkstra's shunting-yard algorithm: operands go straight to the output, and operators wait on a stack until an operator of lower precedence, or a closing parenthesis, forces them out. ^ is right-associative (a^b^c means a^(b^c)); the others are left-associative.
Example: a+b*(c^d-e)^(f+g*h)-i → abcd^e-fgh*+^*+i-.
string infixToPostfix(const string& s); // single-character operands; + - * / ^ and parentheses
| Input | Output | Explanation |
|---|---|---|
a+b*(c^d-e)^(f+g*h)-i | abcd^e-fgh*+^*+i- | |
A*(B+C)/D | ABC+*D/ | |
a-b+c | ab-c+ | Left-associative: (a−b)+c |
a^b^c | abc^^ | Right-associative: a^(b^c) |
(a+b)*(c-d) | ab+cd-* |
(Confirm: operands are single letters or digits; the expression is valid; precedence is ^ > * / > + −, with ^ right-associative.)
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 interviewAll 474 dsa interview questions
DSA mock interviews · System design interview questions · Free mock interview practice · Browse interviewers · Sign in