📖 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, then write code to check if the stack is empty or full.
📋 What You'll Learn
Create an integer array called stack with size 5
Create an integer variable called top initialized to -1
Create an integer variable called maxSize set to 5
Write a condition to check if the stack is empty using top
Write a condition to check if the stack is full using top and maxSize
Print "Stack is empty" if empty
Print "Stack is full" if full
Print "Stack is neither empty nor full" otherwise
💡 Why This Matters
🌍 Real World
Stacks are used in many real-world applications like undo features in text editors, browser history, and managing function calls in programming.
💼 Career
Understanding stack operations and conditions is essential for software developers, especially when working with algorithms, memory management, and system design.
Progress0 / 4 steps
1
Create the stack data structure
Create an integer array called stack with size 5 and an integer variable called top initialized to -1.
DSA C#
Hint
Use int[] stack = new int[5]; to create the stack array and int top = -1; to mark it empty.
2
Add the maximum size variable
Create an integer variable called maxSize and set it to 5.
DSA C#
Hint
Use int maxSize = 5; to store the maximum stack size.
3
Check if the stack is empty or full
Write an if statement to check if the stack is empty by comparing top to -1, and an else if statement to check if the stack is full by comparing top to maxSize - 1.
DSA C#
Hint
Use if (top == -1) to check empty and else if (top == maxSize - 1) to check full.
4
Print the stack state
Print "Stack is empty" if empty, "Stack is full" if full, and "Stack is neither empty nor full" otherwise using System.Console.WriteLine.
DSA C#
Hint
Run the program to see the printed stack state.
Practice
(1/5)
1. What does the IsEmpty() method typically 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() method checks whether the stack contains any elements or not.
Step 2: Interpret the meaning of empty 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() = true means stack is empty [OK]
Hint: Empty means zero elements inside the stack [OK]
Common Mistakes:
Confusing empty with full
Thinking IsEmpty checks for one element
Assuming IsEmpty checks capacity
2. Which of the following is the correct C# syntax to check if a stack named myStack is full assuming it has a fixed size?
easy
A. if (myStack.Count == myStack.Capacity) { }
B. if (myStack.IsEmpty()) { }
C. if (myStack.Length == 0) { }
D. if (myStack.IsFull()) { }
Solution
Step 1: Understand stack properties in C#
A stack with fixed size uses Count for current elements and Capacity for max size.
Step 2: Check if stack is full
If Count equals Capacity, the stack is full and cannot add more elements.
Final Answer:
if (myStack.Count == myStack.Capacity) { } -> Option A
Quick Check:
Count == Capacity means full stack [OK]
Hint: Full means count equals capacity in fixed size stack [OK]
Three elements (10, 20, 30) are pushed into a stack with capacity 3, so stack is full.
Step 2: Evaluate isFull and isEmpty values
isFull = stack.Count == 3 is true; isEmpty = stack.Count == 0 is false.
Final Answer:
True\nFalse -> Option B
Quick Check:
Count=3 means full=true, empty=false [OK]
Hint: Count equals capacity means full; zero means empty [OK]
Common Mistakes:
Assuming stack is empty after pushes
Confusing isFull and isEmpty values
Ignoring stack capacity
4. The following C# code tries to check if a stack is full but causes a compile error:
Stack<int> stack = new Stack<int>();
if (stack.IsFull()) {
Console.WriteLine("Stack is full");
}
What is the error and how to fix it?
medium
A. Change Stack<int> to List<int>
B. Stack cannot be empty; remove the if condition
C. IsFull() method does not exist; use stack.Count == capacity instead
D. Use stack.IsEmpty() instead of IsFull()
Solution
Step 1: Identify the error
The IsFull() method is not defined in C# Stack class, causing a compile error.
Step 2: Correct the check for full stack
Use stack.Count == capacity to check if stack is full, since Stack has no IsFull() method.
Final Answer:
IsFull() method does not exist; use stack.Count == capacity instead -> Option C
Quick Check:
No IsFull() method in Stack class [OK]
Hint: Stack class has no IsFull(); check count vs capacity [OK]
Common Mistakes:
Assuming IsFull() exists by default
Using IsEmpty() to check full
Changing data structure unnecessarily
5. You have a fixed size stack of capacity 5. After pushing 3 elements, you want to check if you can push 2 more without overflow. Which condition correctly checks this in C#?
hard
A. if (stack.Count - 2 >= 0) { /* safe to push */ }
B. if (stack.IsEmpty()) { /* safe to push */ }
C. if (stack.Count == 5) { /* safe to push */ }
D. if (stack.Count + 2 <= 5) { /* safe to push */ }
Solution
Step 1: Understand stack capacity and current count
Stack capacity is 5, current count is 3, so 2 more pushes are possible if total does not exceed 5.
Step 2: Formulate condition to check safe push
Check if current count plus 2 is less than or equal to capacity: stack.Count + 2 <= 5.
Final Answer:
if (stack.Count + 2 <= 5) { /* safe to push */ } -> Option D
Quick Check:
Count + pushes ≤ capacity means safe [OK]
Hint: Add pushes to count; compare with capacity [OK]
Common Mistakes:
Using IsEmpty() to check push safety
Checking count equals capacity incorrectly
Subtracting pushes instead of adding
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.