Bird
Raised Fist0
DSA Goprogramming~10 mins

Infix to Postfix Conversion Using Stack in DSA Go - Interactive Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to push an element onto the stack.

DSA Go
func push(stack []rune, element rune) []rune {
    stack = append(stack, [1])
    return stack
}
Drag options to blanks, or click blank then click option'
Aelement
Bstack
Crune
Dappend
Attempts:
3 left
💡 Hint
Common Mistakes
Appending the whole stack instead of the element.
Using the wrong variable name.
Trying to append the type 'rune' instead of the variable.
2fill in blank
medium

Complete the code to check if the stack is empty.

DSA Go
func isEmpty(stack []rune) bool {
    return len(stack) [1] 0
}
Drag options to blanks, or click blank then click option'
A!=
B>
C<
D==
Attempts:
3 left
💡 Hint
Common Mistakes
Using '!=' instead of '=='.
Using '>' or '<' which do not correctly check emptiness.
3fill in blank
hard

Fix the error in popping the top element from the stack.

DSA Go
func pop(stack []rune) ([]rune, rune) {
    if isEmpty(stack) {
        return stack, 0
    }
    top := stack[len(stack)[1]]
    stack = stack[:len(stack)-1]
    return stack, top
}
Drag options to blanks, or click blank then click option'
A0
B+1
C-1
D-2
Attempts:
3 left
💡 Hint
Common Mistakes
Using len(stack) which is out of range.
Using len(stack)+1 which causes runtime error.
4fill in blank
hard

Fill both blanks to correctly check operator precedence.

DSA Go
func precedence(op rune) int {
    if op == '+' || op == '-' {
        return [1]
    } else if op == '*' || op == '/' {
        return [2]
    }
    return 0
}
Drag options to blanks, or click blank then click option'
A1
B2
C0
D3
Attempts:
3 left
💡 Hint
Common Mistakes
Assigning higher precedence to '+' than '*'.
Using zero for all operators.
5fill in blank
hard

Fill all three blanks to correctly convert infix to postfix inside the loop.

DSA Go
for _, ch := range expression {
    if (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') {
        postfix += string([1])
    } else if ch == '(' {
        stack = push(stack, [2])
    } else if ch == ')' {
        for !isEmpty(stack) && stack[len(stack)-1] != [3] {
            var top rune
            stack, top = pop(stack)
            postfix += string(top)
        }
        stack, _ = pop(stack) // pop '('
    }
}
Drag options to blanks, or click blank then click option'
Ach
B'('
C')'
D'+'
Attempts:
3 left
💡 Hint
Common Mistakes
Pushing wrong character instead of '('.
Comparing with wrong character in the loop condition.
Adding wrong character to postfix.

Practice

(1/5)
1. What is the main purpose of using a stack in Infix to Postfix Conversion?
easy
A. To temporarily hold operators and manage their precedence
B. To store the final postfix expression
C. To reverse the infix expression
D. To count the number of operands

Solution

  1. Step 1: Understand the role of stack in conversion

    The stack is used to hold operators temporarily while processing the infix expression to respect operator precedence and associativity.
  2. Step 2: Identify what is stored in stack

    Operands are directly added to the output, while operators are pushed to the stack to decide their order in postfix.
  3. Final Answer:

    To temporarily hold operators and manage their precedence -> Option A
  4. Quick Check:

    Stack holds operators for precedence [OK]
Hint: Stack holds operators, output holds operands [OK]
Common Mistakes:
  • Thinking stack stores operands
  • Confusing stack role with output
  • Ignoring operator precedence
2. Which of the following Go code snippets correctly initializes an empty stack for infix to postfix conversion?
easy
A. var stack []rune = []rune{}
B. stack := make([]int, 0)
C. stack := []string{0}
D. var stack []int = nil

Solution

  1. Step 1: Identify correct stack type for operators

    Operators are characters, so stack should be a slice of runes (characters) initialized empty.
  2. Step 2: Check initialization correctness

    var stack []rune = []rune{} initializes an empty rune slice correctly. stack := make([]int, 0) uses int slice, not suitable. stack := []string{0} has syntax error (cannot use int 0 as string). var stack []int = nil is nil slice, which can cause runtime errors.
  3. Final Answer:

    var stack []rune = []rune{} -> Option A
  4. Quick Check:

    Empty rune slice for operator stack [OK]
Hint: Use rune slice for operator stack [OK]
Common Mistakes:
  • Using int slice for operators
  • Initializing with wrong type
  • Using nil slice without initialization
3. What is the postfix expression output by the following Go code snippet for input "a+b*c"?
infix := "a+b*c"
// Assume correct infix to postfix conversion function called here
postfix := InfixToPostfix(infix)
fmt.Println(postfix)
medium
A. "a+bc*"
B. "ab+c*"
C. "abc*+"
D. "*abc+"

Solution

  1. Step 1: Analyze operator precedence in expression

    Multiplication (*) has higher precedence than addition (+), so 'b*c' is evaluated before adding 'a'.
  2. Step 2: Convert infix to postfix step-by-step

    Postfix is formed as 'a' 'b' 'c' '*' '+', which is "abc*+".
  3. Final Answer:

    "abc*+" -> Option C
  4. Quick Check:

    Multiplication before addition = "abc*+" [OK]
Hint: Multiply before add means * goes before + in postfix [OK]
Common Mistakes:
  • Ignoring operator precedence
  • Placing + before * in postfix
  • Confusing operand order
4. Given this Go function snippet for infix to postfix conversion, what is the bug?
func precedence(op rune) int {
    if op == '+' || op == '-' {
        return 1
    } else if op == '*' || op == '/' {
        return 2
    }
    return 0
}

// InfixToPostfix function uses precedence(op) to compare operators
// but outputs incorrect postfix for input "a+b^c"
medium
A. Precedence function returns 0 for non-operators causing errors
B. Precedence function returns int instead of float
C. Precedence values for '+' and '-' are swapped
D. Precedence function does not handle '^' operator

Solution

  1. Step 1: Check if all operators are handled

    The function handles '+', '-', '*', '/' but misses '^' (exponent) operator which is common in infix expressions.
  2. Step 2: Understand impact on output

    If input contains '^', missing precedence causes wrong order in postfix output.
  3. Final Answer:

    Precedence function does not handle '^' operator -> Option D
  4. Quick Check:

    Missing '^' causes wrong postfix [OK]
Hint: Include all operators in precedence function [OK]
Common Mistakes:
  • Ignoring missing operators
  • Assuming 0 precedence is error
  • Confusing precedence values
5. How would you modify the infix to postfix algorithm to correctly handle parentheses in the expression (a+b)*c?
hard
A. Ignore parentheses and treat them as operands
B. Push '(' onto stack and pop operators until '(' when ')' is found
C. Remove all parentheses before conversion
D. Treat '(' as higher precedence operator and never pop it

Solution

  1. Step 1: Understand role of parentheses in infix

    Parentheses change normal precedence by grouping expressions to evaluate first.
  2. Step 2: Modify stack operations for parentheses

    Push '(' onto stack when encountered. When ')' is found, pop operators until '(' is popped, discarding both parentheses.
  3. Final Answer:

    Push '(' onto stack and pop operators until '(' when ')' is found -> Option B
  4. Quick Check:

    Parentheses control stack push/pop [OK]
Hint: Push '(' and pop until '(' on ')' [OK]
Common Mistakes:
  • Ignoring parentheses in algorithm
  • Treating parentheses as operands
  • Not popping until '(' on ')'
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard