Bird
Raised Fist0
DSA C#programming~10 mins

Check if Stack is Empty or Full in DSA C# - Execution Trace

Choose your learning style10 modes available
Concept Flow - Check if Stack is Empty or Full
Start
↓
Check if stack pointer == -1?
↓
Stack is [Check if stack pointer == max size - 1?
↓
Stack is [Stack has space
The flow checks if the stack pointer is -1 to decide if the stack is empty. If not empty, it checks if the pointer reached max size - 1 to decide if the stack is full.
Execution Sample
DSA C#
int top = -1;
int maxSize = 3;

bool IsEmpty() => top == -1;
bool IsFull() => top == maxSize - 1;
This code checks if the stack is empty or full by comparing the top pointer with -1 and maxSize - 1.
Execution Table
StepOperationTop PointerMax SizeIsEmpty ResultIsFull ResultVisual Stack State
1Initialize stack-13TrueFalse[] (empty)
2Push 1003FalseFalse[10]
3Push 2013FalseFalse[10, 20]
4Push 3023FalseTrue[10, 20, 30]
5Check IsEmpty23FalseTrue[10, 20, 30]
6Check IsFull23FalseTrue[10, 20, 30]
7Pop one element13FalseFalse[10, 20]
8Check IsEmpty13FalseFalse[10, 20]
9Check IsFull13FalseFalse[10, 20]
10Pop all elements-13TrueFalse[] (empty)
11Check IsEmpty-13TrueFalse[] (empty)
12Check IsFull-13TrueFalse[] (empty)
💡 Execution stops after stack is emptied and checks confirm empty and not full.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 7After Step 10Final
top-10121-1-1
maxSize3333333
IsEmpty()TrueFalseFalseFalseFalseTrueTrue
IsFull()FalseFalseFalseTrueFalseFalseFalse
Stack State[][10][10, 20][10, 20, 30][10, 20][][]
Key Moments - 3 Insights
Why does the stack pointer start at -1 to indicate empty?
Because the top pointer points to the last filled position, -1 means no elements are present. See Step 1 in execution_table where top = -1 means empty stack.
Why is the stack full when top equals maxSize - 1?
Because array indices start at 0, so maxSize - 1 is the last valid index. At Step 4, top = 2 and maxSize = 3 means stack is full.
What happens to IsEmpty and IsFull after popping all elements?
After popping all, top returns to -1, so IsEmpty becomes True and IsFull becomes False as shown in Steps 10-12.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at Step 4, what is the IsFull result?
ATrue
BFalse
CUndefined
DError
💡 Hint
Check the 'IsFull Result' column at Step 4 in execution_table.
At which step does the stack become empty again after popping?
AStep 7
BStep 5
CStep 10
DStep 3
💡 Hint
Look at 'Top Pointer' and 'Stack State' columns in execution_table to find when top returns to -1.
If maxSize was increased to 4, what would be the IsFull result at Step 4?
ATrue
BFalse
CError
DDepends on top
💡 Hint
IsFull checks if top == maxSize - 1. Increasing maxSize to 4 means top=2 is not full yet.
Concept Snapshot
Stack uses a pointer 'top' to track last element.
Empty stack: top == -1.
Full stack: top == maxSize - 1.
IsEmpty() returns true if top is -1.
IsFull() returns true if top reaches maxSize - 1.
Push increments top; Pop decrements top.
Full Transcript
This concept shows how to check if a stack is empty or full using the top pointer. The top starts at -1 when empty. When pushing elements, top increases. When top equals maxSize - 1, the stack is full. The code uses simple comparisons to check these states. The execution table traces pushing three elements, checking empty/full states, popping elements, and checking again. The variable tracker shows how top and stack state change step by step. Key moments clarify why top starts at -1 and how full condition works. The quiz tests understanding of these states at different steps.

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

  1. Step 1: Understand the purpose of IsEmpty()

    The IsEmpty() method checks whether the stack contains any elements or not.
  2. Step 2: Interpret the meaning of empty stack

    An empty stack means it has zero elements inside, so no items to pop or peek.
  3. Final Answer:

    If the stack has no elements inside -> Option A
  4. 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

  1. Step 1: Understand stack properties in C#

    A stack with fixed size uses Count for current elements and Capacity for max size.
  2. Step 2: Check if stack is full

    If Count equals Capacity, the stack is full and cannot add more elements.
  3. Final Answer:

    if (myStack.Count == myStack.Capacity) { } -> Option A
  4. Quick Check:

    Count == Capacity means full stack [OK]
Hint: Full means count equals capacity in fixed size stack [OK]
Common Mistakes:
  • Using IsEmpty() to check full
  • Checking Length property which stacks don't have
  • Using IsFull() without defining it
3. Consider this C# code snippet:
Stack<int> stack = new Stack<int>(3);
stack.Push(10);
stack.Push(20);
stack.Push(30);
bool isFull = stack.Count == 3;
bool isEmpty = stack.Count == 0;
Console.WriteLine(isFull);
Console.WriteLine(isEmpty);

What will be the output?
medium
A. True\nTrue
B. True\nFalse
C. False\nTrue
D. False\nFalse

Solution

  1. Step 1: Analyze stack operations

    Three elements (10, 20, 30) are pushed into a stack with capacity 3, so stack is full.
  2. Step 2: Evaluate isFull and isEmpty values

    isFull = stack.Count == 3 is true; isEmpty = stack.Count == 0 is false.
  3. Final Answer:

    True\nFalse -> Option B
  4. 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

  1. Step 1: Identify the error

    The IsFull() method is not defined in C# Stack class, causing a compile error.
  2. Step 2: Correct the check for full stack

    Use stack.Count == capacity to check if stack is full, since Stack has no IsFull() method.
  3. Final Answer:

    IsFull() method does not exist; use stack.Count == capacity instead -> Option C
  4. 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

  1. 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.
  2. 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.
  3. Final Answer:

    if (stack.Count + 2 <= 5) { /* safe to push */ } -> Option D
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard