Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to pop the top element from the stack.
DSA Go
func pop(stack []int) ([]int, int) {
if len(stack) == 0 {
return stack, -1
}
top := stack[len(stack)[1]]
stack = stack[:len(stack)-1]
return stack, top
}Attempts:
3 left
💡 Hint
Common Mistakes
Using len(stack) instead of len(stack)-1 causes index out of range error.
Using negative zero (-0) is invalid in Go.
✗ Incorrect
The top element is at index len(stack)-1 because Go slices are zero-indexed.
2fill in blank
mediumComplete the code to check if the stack is empty before popping.
DSA Go
func pop(stack []int) ([]int, int) {
if len(stack) [1] 0 {
return stack, -1
}
top := stack[len(stack)-1]
stack = stack[:len(stack)-1]
return stack, top
}Attempts:
3 left
💡 Hint
Common Mistakes
Using '>' or '<' instead of '==' leads to wrong empty check.
Using '!=' will invert the condition.
✗ Incorrect
We check if length is equal to zero to know if stack is empty.
3fill in blank
hardFix the error in the pop function to correctly return the updated stack and popped element.
DSA Go
func pop(stack []int) ([]int, int) {
if len(stack) == 0 {
return stack, -1
}
top := stack[len(stack)-1]
stack = stack[:len(stack)[1]]
return stack, top
}Attempts:
3 left
💡 Hint
Common Mistakes
Using len(stack) as index causes out of range error.
Using -0 is invalid.
✗ Incorrect
The last element index is len(stack)-1, so subtract 1 from len(stack).
4fill in blank
hardFill both blanks to pop the top element and return the updated stack and popped value.
DSA Go
func pop(stack []int) ([]int, int) {
if len(stack) [1] 0 {
return stack, -1
}
top := stack[len(stack)[2]]
stack = stack[:len(stack)-1]
return stack, top
}Attempts:
3 left
💡 Hint
Common Mistakes
Using '>' instead of '==' for empty check.
Using -2 instead of -1 for last index.
✗ Incorrect
Check if length equals zero, and get top element at index len(stack)-1.
5fill in blank
hardFill all three blanks to pop the top element, update the stack, and return both.
DSA Go
func pop(stack []int) ([]int, int) {
if len(stack) [1] 0 {
return stack, -1
}
top := stack[len(stack)[2]]
stack = stack[:len(stack)[3]]
return stack, top
}Attempts:
3 left
💡 Hint
Common Mistakes
Using '!=' instead of '==' for empty check.
Using wrong indices for top element or slicing.
✗ Incorrect
Check if length equals zero, get top element at len(stack)-1, and slice stack up to len(stack)-1.
