📖 Scenario: You are building a simple stack to manage plates in a kitchen. The stack can hold only a limited number of plates. You want to check if the stack is empty (no plates) or full (maximum plates) before adding or removing plates.
🎯 Goal: Create a stack with a fixed size, add a configuration for maximum capacity, write functions to check if the stack is empty or full, and print the results.
📋 What You'll Learn
Create a stack as a slice of integers with exact values
Create a variable for maximum stack capacity
Write functions named isEmpty and isFull to check stack state
Print the results of isEmpty and isFull functions
💡 Why This Matters
🌍 Real World
Stacks are used in many real-world applications like undo features in text editors, managing function calls, and plate stacks in kitchens.
💼 Career
Understanding stack operations and state checks is fundamental for software developers working with data structures and algorithms.
Progress0 / 4 steps
1
Create the initial stack
Create a variable called stack as a slice of integers with these exact values: 10, 20, 30.
DSA Go
Hint
Use stack := []int{10, 20, 30} to create the stack slice.
2
Add maximum capacity configuration
Add a variable called maxCapacity and set it to 5 to represent the maximum number of plates the stack can hold.
DSA Go
Hint
Use maxCapacity := 5 to set the maximum capacity.
3
Write functions to check if stack is empty or full
Write two functions inside main: isEmpty that returns true if len(stack) == 0, and isFull that returns true if len(stack) == maxCapacity.
DSA Go
Hint
Define isEmpty and isFull as functions returning boolean values based on the length of stack.
4
Print if the stack is empty or full
Use fmt.Println to print the results of calling isEmpty() and isFull() functions.
DSA Go
Hint
Use fmt.Println("Is stack empty?", isEmpty()) and fmt.Println("Is stack full?", isFull()) to print the results.
Practice
(1/5)
1. What does the IsEmpty() function check in a stack data structure?
easy
A. If the stack has no elements inside
B. If the stack is full and cannot add more elements
C. If the stack has exactly one element
D. If the stack has more than one element
Solution
Step 1: Understand the purpose of IsEmpty()
The IsEmpty() function checks whether the stack contains any elements or not.
Step 2: Identify what empty means for a stack
An empty stack means it has zero elements inside, so no items to pop or peek.
Final Answer:
If the stack has no elements inside -> Option A
Quick Check:
IsEmpty() means stack is empty = true [OK]
Hint: Empty means zero elements inside stack [OK]
Common Mistakes:
Confusing empty with full
Thinking empty means one element
Mixing IsEmpty with IsFull
2. Which of the following is the correct Go syntax to check if a stack is full when using a fixed size array?
easy
A. if len(stack) == 0 { /* stack is full */ }
B. if top > len(stack) { /* stack is full */ }
C. if top == len(stack) - 1 { /* stack is full */ }
D. if top == 0 { /* stack is full */ }
Solution
Step 1: Understand stack indexing in Go
For a fixed size array stack, top points to the last filled index. The last valid index is length - 1.
Step 2: Check condition for full stack
When top equals len(stack) - 1, the stack is full because no more elements can be added.
Final Answer:
if top == len(stack) - 1 { /* stack is full */ } -> Option C
Quick Check:
top == max index means full [OK]
Hint: Full means top equals last index (length - 1) [OK]
Common Mistakes:
Using > instead of == for full check
Checking if top == 0 for full
Confusing length with top index
3. Given the following Go code, what will be printed?
type Stack struct {
items []int
top int
}
func (s *Stack) IsEmpty() bool {
return s.top == -1
}
func main() {
s := Stack{items: make([]int, 5), top: -1}
fmt.Println(s.IsEmpty())
s.top = 4
fmt.Println(s.IsEmpty())
}
medium
A. true\nfalse
B. false\ntrue
C. false\nfalse
D. true\ntrue
Solution
Step 1: Check initial top value and IsEmpty() result
Initially, top is -1, so IsEmpty() returns true because stack is empty.
Step 2: Update top to 4 and check IsEmpty() again
After setting top to 4, IsEmpty() returns false because stack now has elements.
Final Answer:
true\nfalse -> Option A
Quick Check:
top -1 means empty true, top 4 means not empty false [OK]
Hint: top -1 means empty, any other top means not empty [OK]
Common Mistakes:
Assuming top 4 means empty
Confusing slice length with top
Ignoring initial top value
4. Find the error in this Go code snippet that checks if a stack is full:
A. The function should return 's.top > len(s.items)' instead
B. The condition should be 's.top == len(s.items) - 1' to check full correctly
C. The function should check if 's.top == 0' to be full
D. No error, the code is correct
Solution
Step 1: Understand stack indexing for full condition
Stack top index starts at -1 and max valid index is length - 1.
Step 2: Identify correct full condition
Full means top equals last valid index, which is len(s.items) - 1, not len(s.items).
Final Answer:
The condition should be 's.top == len(s.items) - 1' to check full correctly -> Option B
Quick Check:
Full means top == max index (length - 1) [OK]
Hint: Full means top equals last index, not length [OK]
Common Mistakes:
Using length instead of length - 1
Checking top == 0 for full
Returning wrong boolean condition
5. You have a stack implemented with a fixed size array of length 3 and a top index starting at -1. After pushing two elements, you pop one element, then push two more elements. Which statement about the stack state is correct?
hard
A. The stack is empty and top index is -1
B. The stack is not full and top index is 1
C. The stack is full but top index is 3
D. The stack is full and top index is 2
Solution
Step 1: Track stack operations and top index changes
Start: top = -1 (empty). Push two elements: top = 1. Pop one element: top = 0. Push two elements: top = 2 (max index for length 3).
Step 2: Determine if stack is full
Since top = 2 and max index is 2 (length 3 - 1), stack is full.
Final Answer:
The stack is full and top index is 2 -> Option D
Quick Check:
Push/pop changes top; top 2 means full for size 3 [OK]
Hint: Track top index after each push/pop carefully [OK]
Common Mistakes:
Assuming top can be 3 for full
Forgetting pop decreases top
Confusing empty with full states
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.