Challenge - 5 Problems
Infix to Postfix Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the output of this infix to postfix conversion?
Given the infix expression
A + B * C, what is the postfix expression produced by the standard stack-based algorithm?DSA Javascript
function infixToPostfix(expression) {
const precedence = {'+':1, '-':1, '*':2, '/':2};
const stack = [];
let result = '';
for (let char of expression.replace(/\s+/g, '')) {
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
In infix to postfix conversion, operators with higher precedence are placed after their operands first. Here, B * C is evaluated before adding A, so the postfix is ABC*+.
❓ Predict Output
intermediateOutput of infix to postfix for expression with parentheses
What is the postfix expression for
(A + B) * C using the stack-based infix to postfix algorithm?DSA Javascript
function infixToPostfix(expression) {
const precedence = {'+':1, '-':1, '*':2, '/':2};
const stack = [];
let result = '';
for (let char of expression.replace(/\s+/g, '')) {
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
Parentheses force the addition to happen before multiplication. So A and B are added first, then multiplied by C, resulting in AB+C*.
🧠 Conceptual
advancedWhich operator has the highest precedence in this infix to postfix algorithm?
In the given infix to postfix conversion code, which operator will be popped from the stack first when the current operator is '*' and the top of the stack is '+'?
DSA Javascript
const precedence = {'+':1, '-':1, '*':2, '/':2};Attempts:
2 left
💡 Hint
Check the condition that decides when to pop from the stack.
✗ Incorrect
The algorithm pops from the stack only if the top operator has precedence greater or equal to the current operator. Since '+' has lower precedence than '*', '*' is pushed without popping '+'.
🔧 Debug
advancedWhat error occurs with this infix to postfix code snippet?
Consider this code snippet for infix to postfix conversion:
while (stack.length && precedence[stack[stack.length - 1]] > precedence[char]) {
result += stack.pop();
}
What problem will this cause when converting A + B * C?DSA Javascript
function infixToPostfix(expression) {
const precedence = {'+':1, '-':1, '*':2, '/':2};
const stack = [];
let result = '';
for (let char of expression.replace(/\s+/g, '')) {
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 for popping operators with equal precedence.
✗ Incorrect
Using '>' instead of '>=' causes operators with equal precedence not to be popped, which can cause wrong order in postfix. For example, '+' is not popped before '*', leading to wrong postfix.
🚀 Application
expertFinal postfix expression after multiple operations
Given the infix expression
A + (B - C) * (D + E) / F, what is the correct postfix expression produced by the stack-based infix to postfix algorithm?DSA Javascript
function infixToPostfix(expression) {
const precedence = {'+':1, '-':1, '*':2, '/':2};
const stack = [];
let result = '';
for (let char of expression.replace(/\s+/g, '')) {
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 && stack[stack.length - 1] !== '(' && 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) * (D + E) / F'));Attempts:
2 left
💡 Hint
Break down the expression by parentheses and operator precedence step by step.
✗ Incorrect
The postfix expression is 'ABC- DE+ * F / +'. The subexpressions inside parentheses are converted first, then multiplication and division, then addition.
