Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to push a value onto the stack.
DSA Go
func (s *MinStack) Push(val int) {
s.stack = append(s.stack, val)
if len(s.minStack) == 0 || val [1] s.minStack[len(s.minStack)-1] {
s.minStack = append(s.minStack, val)
} else {
s.minStack = append(s.minStack, s.minStack[len(s.minStack)-1])
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Using '>' instead of '<=' causes incorrect minimum tracking.
Not handling empty minStack leads to errors.
✗ Incorrect
We push the new value onto minStack if it is less than or equal to the current minimum to keep track of the minimum value.
2fill in blank
mediumComplete the code to pop a value from the stack.
DSA Go
func (s *MinStack) Pop() {
if len(s.stack) == 0 {
return
}
s.stack = s.stack[:len(s.stack)[1]1]
s.minStack = s.minStack[:len(s.minStack)[1]1]
}Attempts:
3 left
💡 Hint
Common Mistakes
Using '+' causes runtime errors.
Using '*' or '/' is invalid for slicing.
✗ Incorrect
To pop, we reduce the slice length by 1 using slicing with minus 1.
3fill in blank
hardFix the error in the GetMin method to return the current minimum.
DSA Go
func (s *MinStack) GetMin() int {
if len(s.minStack) == 0 {
return 0
}
return s.minStack[1]len(s.minStack)-1]
}Attempts:
3 left
💡 Hint
Common Mistakes
Using parentheses or braces causes syntax errors.
Using '<' is invalid syntax.
✗ Incorrect
To access the last element of a slice, use square brackets [].
4fill in blank
hardComplete the code to complete the Peek method that returns the top element of the stack.
DSA Go
func (s *MinStack) Peek() int {
if len(s.stack) == 0 {
return -1
}
return s.stack[len(s.stack)[1]1]
}Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to subtract 1 causes out-of-range errors.
Missing the closing bracket causes syntax errors.
✗ Incorrect
To get the last element, subtract 1 from length and close the index with a square bracket.
5fill in blank
hardFill all three blanks to complete the MinStack struct and its constructor.
DSA Go
type MinStack struct {
stack [][1]
minStack [][2]
}
func NewMinStack() *MinStack {
return &MinStack{
stack: make([][3], 0),
minStack: make([]int, 0),
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Using string or float64 causes type mismatch errors.
Using bool is incorrect for numeric stacks.
✗ Incorrect
Both stack and minStack hold integers, so the type is int.
