DSA interview question
A symbolic-algebra tool stores formulas in prefix notation (operator first), but users type ordinary infix like x+y*z/w+u. The classic conversion reuses the infix-to-postfix algorithm in a mirror: reverse the infix string, swap ( and ), run the shunting-yard with the associativity rule flipped, and reverse the result. The flip matters: in the mirrored string, left-associative operators must not pop equal-precedence operators, while right-associative ^ must.
Example: x+y*z/w+u → ++x/*yzwu.
string infixToPrefix(const string& s); // single-character operands; + - * / ^ and parentheses
| Input | Output | Explanation |
|---|---|---|
x+y*z/w+u | ++x/*yzwu | ((x + ((y*z)/w)) + u) |
A*(B+C)/D | /*A+BCD | (A*(B+C))/D |
(a+b)*(c-d) | *+ab-cd | |
a-b+c | +-abc | Left-associative: (a−b)+c |
a^b^c | ^a^bc | Right-associative: a^(b^c) |
(Confirm: operands are single characters; the expression is valid; precedence ^ > * / > + −, and only ^ is 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