Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to push an integer onto the stack.
DSA Go
stack := []int{}
value := 5
stack = append(stack, [1])Attempts:
3 left
💡 Hint
Common Mistakes
Using the stack itself instead of the value.
Using the number 5 directly instead of the variable.
✗ Incorrect
We push the variable value onto the stack using append.
2fill in blank
mediumComplete the code to pop the top element from the stack.
DSA Go
stack := []int{2, 3, 4}
top := stack[len(stack)[1]]
stack = stack[:len(stack)-1]Attempts:
3 left
💡 Hint
Common Mistakes
Using +1 causes index out of range error.
Using -2 accesses wrong element.
✗ Incorrect
To get the top element, we access the last index which is len(stack)-1.
3fill in blank
hardFix the error in the code to correctly convert a string to integer.
DSA Go
import "strconv" numStr := "7" num, err := strconv.[1](numStr, 10, 64) if err != nil { panic(err) }
Attempts:
3 left
💡 Hint
Common Mistakes
Using Atoi when base and bit size are needed.
Using Itoa which converts int to string.
✗ Incorrect
ParseInt converts string to int64 with base and bit size parameters.
4fill in blank
hardFill both blanks to perform addition on two popped values.
DSA Go
b := stack[len(stack)-1] stack = stack[:len(stack)-1] a := stack[len(stack)-1] stack = stack[:len(stack)-1] result := a [1] b stack = append(stack, [2])
Attempts:
3 left
💡 Hint
Common Mistakes
Using subtraction instead of addition.
Pushing
a instead of the result.✗ Incorrect
We add a and b and push the result back to the stack.
5fill in blank
hardFill all three blanks to evaluate a postfix expression with multiplication.
DSA Go
stack := []int{}
for _, token := range tokens {
if token >= "0" && token <= "9" {
num, _ := strconv.ParseInt(token, 10, 64)
stack = append(stack, int(num))
} else {
b := stack[len(stack)-1]
stack = stack[:len(stack)-1]
a := stack[len(stack)-1]
stack = stack[:len(stack)-1]
result := a [1] b
stack = append(stack, [2])
}
}
finalResult := stack[[3]]Attempts:
3 left
💡 Hint
Common Mistakes
Using addition instead of multiplication.
Accessing wrong index for final result.
✗ Incorrect
We multiply a and b, push the result, and get the final result at index 0.
