Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to check if the stack is empty.
DSA Go
func (s *Stack) IsEmpty() bool {
return s.top [1] -1
}Attempts:
3 left
💡 Hint
Common Mistakes
Using > or < instead of == causes wrong empty check.
Using != will be true even when stack has elements.
✗ Incorrect
The stack is empty when the top index is exactly -1.
2fill in blank
mediumComplete the code to check if the stack is full.
DSA Go
func (s *Stack) IsFull() bool {
return s.top [1] s.size-1
}Attempts:
3 left
💡 Hint
Common Mistakes
Using < or > causes incorrect full check.
Using != will be true even when stack is not full.
✗ Incorrect
The stack is full when the top index equals size-1.
3fill in blank
hardFix the error in the pop method to prevent popping from an empty stack.
DSA Go
func (s *Stack) Pop() (int, error) {
if s.top [1] -1 {
return 0, errors.New("stack is empty")
}
val := s.items[s.top]
s.top--
return val, nil
}Attempts:
3 left
💡 Hint
Common Mistakes
Using > or != causes wrong empty detection.
Using <= allows popping when stack is empty.
✗ Incorrect
Pop should check if top == -1 to detect empty stack before popping.
4fill in blank
hardFill both blanks to complete the push method with full stack check.
DSA Go
func (s *Stack) Push(val int) error {
if s.top [1] s.size-1 {
return errors.New("stack is full")
}
s.top[2]
s.items[s.top] = val
return nil
}Attempts:
3 left
💡 Hint
Common Mistakes
Using < instead of == causes wrong full check.
Using ++ is invalid in Go; use += 1.
✗ Incorrect
Check if top == size-1 to detect full stack, then increment top by 1 before adding value.
5fill in blank
hardFill all three blanks to create a method that returns stack status as a string.
DSA Go
func (s *Stack) Status() string {
if s.top [1] -1 {
return "Empty"
} else if s.top [2] s.size-1 {
return "Full"
} else {
return "[3]"
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Using != causes wrong empty/full detection.
Returning wrong string for partial status.
✗ Incorrect
Check if top == -1 for empty, top == size-1 for full, else return "Partial".
