Challenge - 5 Problems
Infix to Postfix Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Postfix Conversion for Simple Expression
What is the output of the following TypeScript code that converts an infix expression to postfix notation?
DSA Typescript
function precedence(op: string): number {
if (op === '+' || op === '-') return 1;
if (op === '*' || op === '/') return 2;
return 0;
}
function infixToPostfix(expression: string): string {
const stack: string[] = [];
let result = '';
for (const char of expression) {
if (/[a-z]/.test(char)) {
result += char;
} else if (char === '(') {
stack.push(char);
} else if (char === ')') {
while (stack.length && stack[stack.length - 1] !== '(') {
result += stack.pop();
}
stack.pop();
} else {
while (stack.length && precedence(stack[stack.length - 1]) >= precedence(char)) {
result += stack.pop();
}
stack.push(char);
}
}
while (stack.length) {
result += stack.pop();
}
return result;
}
console.log(infixToPostfix("a+b*c"));Attempts:
2 left
💡 Hint
Remember that multiplication has higher precedence than addition.
✗ Incorrect
The expression 'a+b*c' converts to postfix as 'abc*+'. The multiplication 'b*c' is done first, then added to 'a'.
❓ Predict Output
intermediatePostfix Conversion Output with Parentheses
What is the output of this TypeScript function when converting the infix expression '(a+b)*c' to postfix?
DSA Typescript
function precedence(op: string): number {
if (op === '+' || op === '-') return 1;
if (op === '*' || op === '/') return 2;
return 0;
}
function infixToPostfix(expression: string): string {
const stack: string[] = [];
let result = '';
for (const char of expression) {
if (/[a-z]/.test(char)) {
result += char;
} else if (char === '(') {
stack.push(char);
} else if (char === ')') {
while (stack.length && stack[stack.length - 1] !== '(') {
result += stack.pop();
}
stack.pop();
} else {
while (stack.length && precedence(stack[stack.length - 1]) >= precedence(char)) {
result += stack.pop();
}
stack.push(char);
}
}
while (stack.length) {
result += stack.pop();
}
return result;
}
console.log(infixToPostfix("(a+b)*c"));Attempts:
2 left
💡 Hint
Parentheses change the order of operations.
✗ Incorrect
The expression '(a+b)*c' converts to postfix as 'ab+c*'. The addition inside parentheses is done first, then multiplied by 'c'.
🧠 Conceptual
advancedUnderstanding Stack Usage in Infix to Postfix Conversion
Why do we use a stack data structure when converting an infix expression to postfix notation?
Attempts:
2 left
💡 Hint
Think about how operators are handled in the conversion process.
✗ Incorrect
The stack holds operators temporarily so we can output them in the correct order based on precedence and associativity rules.
🔧 Debug
advancedIdentify the Error in Postfix Conversion Code
What error will the following TypeScript code produce when converting 'a+b*c' to postfix?
DSA Typescript
function precedence(op: string): number {
if (op === '+' || op === '-') return 1;
if (op === '*' || op === '/') return 2;
return 0;
}
function infixToPostfix(expression: string): string {
const stack: string[] = [];
let result = '';
for (const char of expression) {
if (/[a-z]/.test(char)) {
result += char;
} else if (char === '(') {
stack.push(char);
} else if (char === ')') {
while (stack.length && stack[stack.length - 1] !== '(') {
result += stack.pop();
}
stack.pop();
} else {
while (stack.length && precedence(stack[stack.length - 1]) > precedence(char)) {
result += stack.pop();
}
stack.push(char);
}
}
while (stack.length) {
result += stack.pop();
}
return result;
}
console.log(infixToPostfix("a+b*c"));Attempts:
2 left
💡 Hint
Check the condition in the while loop that pops from the stack for operator precedence.
✗ Incorrect
The condition uses '>' instead of '>=' causing operators with equal precedence to not pop correctly, missing the addition operator in output.
🚀 Application
expertPostfix Expression Length After Conversion
Given the infix expression 'a+(b*c-(d/e^f)*g)*h', how many characters will the postfix expression have after conversion using a correct infix to postfix algorithm?
Attempts:
2 left
💡 Hint
Count operands and operators carefully; each operand and operator appears once in postfix.
✗ Incorrect
The infix expression has 8 operands (a,b,c,d,e,f,g,h) and 7 operators (+,*,-,/,^,*,+). Total length = 15.
