Bird
Raised Fist0
DSA Javascriptprogramming~10 mins

Check if Stack is Empty or Full in DSA Javascript - 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-1?
↓
Stack is [Stack has space
We check the stack pointer to see if it is -1 (empty) or max-1 (full) to determine stack state.
Execution Sample
DSA Javascript
const stack = [];
const max = 3;
let top = -1;

// Check empty
const isEmpty = top === -1;
// Check full
const isFull = top === max - 1;
This code checks if the stack is empty or full by comparing the top pointer with -1 and max-1.
Execution Table
StepOperationtop PointerStack ContentIs Empty?Is Full?Visual State
1Initialize stack-1[]truefalseEmpty stack, top = -1
2Push 100[10]falsefalseOne element pushed
3Push 201[10, 20]falsefalseTwo elements pushed
4Push 302[10, 20, 30]falsetrueStack full at max size 3
5Check empty2[10, 20, 30]falsetrueStack is full, not empty
6Pop one element1[10, 20]falsefalseOne element popped, stack not full
7Check empty1[10, 20]falsefalseStack not empty, not full
8Pop two elements-1[]truefalseStack empty after popping all
9Check empty-1[]truefalseStack is empty
10Check full-1[]truefalseStack is empty, so not full
💡 Execution stops after checking stack states post push/pop operations.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 6After Step 8Final
top-10121-1-1
stack[][10][10, 20][10, 20, 30][10, 20][][]
isEmptytruefalsefalsefalsefalsetruetrue
isFullfalsefalsefalsetruefalsefalsefalse
Key Moments - 3 Insights
Why does top start at -1 instead of 0?
Top starts at -1 to indicate the stack is empty before any push. See Step 1 in execution_table where top = -1 means empty stack.
Why is the stack full when top equals max - 1?
Because array indices start at 0, max - 1 is the last valid index. At Step 4, top = 2 with max = 3 means stack is full.
What happens to isEmpty and isFull after popping all elements?
After popping all elements, top returns to -1 (Step 8), so isEmpty becomes true and isFull becomes false, as shown in Steps 9 and 10.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at Step 4, what is the value of isFull?
Afalse
Btrue
Cundefined
Dnull
💡 Hint
Check the 'Is Full?' column at Step 4 in execution_table.
At which step does the stack become empty again after pushing elements?
AStep 6
BStep 4
CStep 8
DStep 3
💡 Hint
Look at the 'top Pointer' and 'Is Empty?' columns in execution_table to find when top returns to -1.
If max was changed to 4, at which top value would the stack be full?
A3
B4
C2
D-1
💡 Hint
Recall that stack is full when top equals max - 1; see concept_flow and execution_table Step 4.
Concept Snapshot
Stack uses a top pointer starting at -1.
Empty stack: top == -1.
Full stack: top == max - 1.
Push increments top; pop decrements top.
Check empty/full by comparing top with -1 and max-1.
Full Transcript
This visualization shows how to check if a stack is empty or full by tracking the top pointer. The top starts at -1 indicating an empty stack. When elements are pushed, top increases. When top reaches max - 1, the stack is full. Popping elements decreases top. The execution table traces these changes step-by-step, showing stack content and pointer values. Key moments clarify why top starts at -1 and how full condition is determined. Quizzes test understanding of these states and pointer values.

Practice

(1/5)
1. What does it mean when a stack is empty?
easy
A. The stack has one element.
B. The stack has reached its maximum size.
C. There are no elements in the stack.
D. The stack is full and cannot add more elements.

Solution

  1. Step 1: Understand the stack empty condition

    A stack is empty when it contains no elements inside it.
  2. Step 2: Compare with other options

    Options about full stack or having elements do not describe empty stack.
  3. Final Answer:

    There are no elements in the stack. -> Option C
  4. Quick Check:

    Empty stack means no elements [OK]
Hint: Empty stack means zero elements inside [OK]
Common Mistakes:
  • Confusing empty with full
  • Thinking empty means one element
  • Mixing empty with partially filled
2. Which of the following JavaScript code snippets correctly checks if a stack array stack is empty?
easy
A. if (stack.length === 0) { /* empty */ }
B. if (stack === null) { /* empty */ }
C. if (stack.length > 0) { /* empty */ }
D. if (stack.size === 0) { /* empty */ }

Solution

  1. Step 1: Check correct property for array length

    JavaScript arrays use length property to get number of elements.
  2. Step 2: Verify condition for empty stack

    Empty means length is zero, so stack.length === 0 is correct.
  3. Final Answer:

    if (stack.length === 0) { /* empty */ } -> Option A
  4. Quick Check:

    Use length property to check empty [OK]
Hint: Use stack.length === 0 to check empty [OK]
Common Mistakes:
  • Using null instead of length
  • Using size property (not for arrays)
  • Checking length > 0 for empty
3. Consider this JavaScript stack implementation with max size 3:
const stack = [];
const maxSize = 3;
stack.push(10);
stack.push(20);
stack.push(30);
const isFull = stack.length === maxSize;
const isEmpty = stack.length === 0;
console.log(isFull, isEmpty);
What will be printed?
medium
A. false false
B. false true
C. true true
D. true false

Solution

  1. Step 1: Trace stack push operations

    Three elements (10, 20, 30) are pushed, so stack length is 3.
  2. Step 2: Evaluate isFull and isEmpty

    isFull = stack.length === maxSize -> 3 === 3 -> true; isEmpty = stack.length === 0 -> 3 === 0 -> false.
  3. Final Answer:

    true false -> Option D
  4. Quick Check:

    Stack full = true, empty = false [OK]
Hint: Stack full if length equals max size [OK]
Common Mistakes:
  • Confusing isFull and isEmpty
  • Forgetting to update length after push
  • Assuming stack is empty after pushes
4. Find the error in this code that checks if a stack is full:
const stack = [1, 2, 3];
const maxSize = 3;
function isStackFull() {
  if (stack.length = maxSize) {
    return true;
  } else {
    return false;
  }
}
medium
A. Using assignment (=) instead of comparison (===) in if condition.
B. maxSize should be stack.length, not a constant.
C. Function should return stack.length instead of boolean.
D. Stack array should be empty to check if full.

Solution

  1. Step 1: Identify the if condition mistake

    The condition uses single equals = which assigns value instead of comparing.
  2. Step 2: Correct comparison operator

    It should use === to compare stack length with maxSize.
  3. Final Answer:

    Using assignment (=) instead of comparison (===) in if condition. -> Option A
  4. Quick Check:

    Use === for comparison, not = [OK]
Hint: Use === for comparison, not = [OK]
Common Mistakes:
  • Using = instead of ===
  • Confusing maxSize with stack length
  • Returning wrong type from function
5. You have a stack with max size 5. After pushing 3 elements, you want to check if the stack is full or empty and print the status as a string: "Full", "Empty", or "Partial". Which code correctly implements this?
hard
A. if (stack.length > 5) { console.log('Full'); } else if (stack.length < 0) { console.log('Empty'); } else { console.log('Partial'); }
B. if (stack.length === 5) { console.log('Full'); } else if (stack.length === 0) { console.log('Empty'); } else { console.log('Partial'); }
C. if (stack.length === 0) { console.log('Full'); } else if (stack.length === 5) { console.log('Empty'); } else { console.log('Partial'); }
D. if (stack.length >= 5) { console.log('Empty'); } else if (stack.length <= 0) { console.log('Full'); } else { console.log('Partial'); }

Solution

  1. Step 1: Check conditions for full and empty stack

    Full means length equals max size (5), empty means length zero.
  2. Step 2: Use else for partial stack

    If neither full nor empty, stack is partially filled.
  3. Final Answer:

    if (stack.length === 5) { console.log('Full'); } else if (stack.length === 0) { console.log('Empty'); } else { console.log('Partial'); } -> Option B
  4. Quick Check:

    Check length === maxSize for full, === 0 for empty [OK]
Hint: Check length === maxSize for full, === 0 for empty [OK]
Common Mistakes:
  • Using wrong comparison operators
  • Swapping full and empty conditions
  • Checking length > maxSize or < 0
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