Bird
Raised Fist0
DSA Typescriptprogramming

Check if Stack is Empty or Full in DSA Typescript

Choose your learning style10 modes available
Mental Model
A stack is like a pile where you add or remove items only from the top. Checking if it is empty means seeing if there is nothing in the pile, and checking if it is full means seeing if the pile has reached its limit.
Analogy: Imagine a stack of plates in a kitchen. You can only add or remove the top plate. If there are no plates, the stack is empty. If the stack reaches the shelf's height limit, it is full.
Top -> [item3] -> [item2] -> [item1] -> null
Index:      2         1         0

Top points to the last added item.
Dry Run Walkthrough
Input: Stack with max size 3, current items: 1, 2 (top is 2). Check if empty and if full.
Goal: Determine if the stack is empty or full at this moment.
Step 1: Check if top index is -1 to see if stack is empty
Top -> [2] -> [1] -> null
Top index = 1
Why: If top is -1, stack is empty; here top is 1, so not empty
Step 2: Check if top index equals max size - 1 to see if stack is full
Top -> [2] -> [1] -> null
Top index = 1, Max size - 1 = 2
Why: If top equals max size - 1, stack is full; here top is 1, max is 2, so not full
Result:
Top -> [2] -> [1] -> null
Stack is not empty
Stack is not full
Annotated Code
DSA Typescript
class Stack {
  private items: number[];
  private maxSize: number;
  private top: number;

  constructor(maxSize: number) {
    this.items = [];
    this.maxSize = maxSize;
    this.top = -1; // top is -1 when stack is empty
  }

  push(item: number): boolean {
    if (this.isFull()) {
      return false; // cannot push, stack full
    }
    this.top++;
    this.items[this.top] = item;
    return true;
  }

  pop(): number | null {
    if (this.isEmpty()) {
      return null; // cannot pop, stack empty
    }
    const popped = this.items[this.top];
    this.top--;
    return popped;
  }

  isEmpty(): boolean {
    return this.top === -1; // top -1 means empty
  }

  isFull(): boolean {
    return this.top === this.maxSize - 1; // top at max index means full
  }

  printStack(): void {
    let result = '';
    for (let i = this.top; i >= 0; i--) {
      result += this.items[i] + (i === 0 ? ' -> null' : ' -> ');
    }
    console.log(result);
  }
}

// Driver code
const stack = new Stack(3);
stack.push(1);
stack.push(2);
stack.printStack();
console.log('Is stack empty? ' + stack.isEmpty());
console.log('Is stack full? ' + stack.isFull());
return this.top === -1; // top -1 means empty
Check if stack has no items by seeing if top is -1
return this.top === this.maxSize - 1; // top at max index means full
Check if stack reached max capacity by comparing top to maxSize - 1
this.top++; this.items[this.top] = item;
Add item on top and move top pointer up
const popped = this.items[this.top]; this.top--;
Remove item from top and move top pointer down
OutputSuccess
2 -> 1 -> null Is stack empty? false Is stack full? false
Complexity Analysis
Time: O(1) because checking top index or updating it takes constant time
Space: O(n) where n is max size, because stack stores up to n items
vs Alternative: Compared to dynamic structures like linked lists, fixed size stack uses fixed space and constant time checks for empty/full
Edge Cases
Empty stack
isEmpty returns true, isFull returns false
DSA Typescript
return this.top === -1; // top -1 means empty
Full stack
isFull returns true, isEmpty returns false
DSA Typescript
return this.top === this.maxSize - 1; // top at max index means full
Single element stack
isEmpty returns false, isFull depends on maxSize
DSA Typescript
return this.top === -1; // top -1 means empty
When to Use This Pattern
When you see a problem asking if a stack can accept more items or if it has any items, reach for checking the top pointer against boundaries because it tells if stack is empty or full instantly.
Common Mistakes
Mistake: Checking if stack is empty by testing if top is 0 instead of -1
Fix: Use top === -1 to indicate empty stack because top starts at -1 before any push
Mistake: Checking if stack is full by comparing top to maxSize instead of maxSize - 1
Fix: Compare top to maxSize - 1 because array indices start at 0
Summary
It checks if a stack has no items (empty) or has reached its capacity (full).
Use it when you need to know if you can add or remove items from a stack safely.
Remember that top being -1 means empty, and top being maxSize - 1 means full.

Practice

(1/5)
1. What does the isEmpty() method check in a stack data structure?
easy
A. If the stack is full and cannot add more elements
B. If the stack has no elements inside
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 is designed to check 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 isEmpty() returns true only when the stack is empty.
  3. Final Answer:

    If the stack has no elements inside -> Option B
  4. Quick Check:

    isEmpty() checks for empty stack = A [OK]
Hint: Empty means zero elements inside the stack [OK]
Common Mistakes:
  • Confusing isEmpty() with isFull()
  • Thinking isEmpty() checks for one element
  • Assuming isEmpty() checks if stack is full
2. Which of the following is the correct TypeScript syntax to check if a stack is full when using an array with fixed size maxSize and current top index?
easy
A. if (top === maxSize - 1) { /* stack is full */ }
B. if (top === maxSize) { /* stack is full */ }
C. if (top > maxSize) { /* stack is full */ }
D. if (top < maxSize) { /* stack is full */ }

Solution

  1. Step 1: Understand stack indexing

    In a fixed size stack using an array, top starts at -1 and increments by 1 for each push. The last valid index is maxSize - 1.
  2. Step 2: Check condition for full stack

    The stack is full when top equals maxSize - 1, meaning no more space to add elements.
  3. Final Answer:

    if (top === maxSize - 1) { /* stack is full */ } -> Option A
  4. Quick Check:

    Full stack when top = maxSize - 1 [OK]
Hint: Stack full when top equals maxSize minus one [OK]
Common Mistakes:
  • Using top === maxSize (off by one error)
  • Checking top > maxSize (invalid index)
  • Using top < maxSize (means not full)
3. Consider this TypeScript code for a stack with maxSize 3:
class Stack {
  maxSize = 3;
  top = -1;
  items: number[] = [];

  isFull() {
    return this.top === this.maxSize - 1;
  }

  push(item: number) {
    if (!this.isFull()) {
      this.top++;
      this.items[this.top] = item;
    }
  }
}

const stack = new Stack();
stack.push(10);
stack.push(20);
stack.push(30);
stack.push(40);
console.log(stack.items);

What will be printed?
medium
A. [40, 20, 30]
B. [10, 20, 30, 40]
C. [10, 20, 30]
D. []

Solution

  1. Step 1: Trace push operations with isFull check

    Stack maxSize is 3, so top max is 2. Push 10: top=0, items[0]=10; Push 20: top=1, items[1]=20; Push 30: top=2, items[2]=30; Push 40: isFull() returns true (top=2), so push ignored.
  2. Step 2: Final stack items array

    After all pushes, items array is [10, 20, 30]. The 4th push does not add 40.
  3. Final Answer:

    [10, 20, 30] -> Option C
  4. Quick Check:

    Stack full at 3 items, 4th push ignored [OK]
Hint: Push stops when stack is full, extra items ignored [OK]
Common Mistakes:
  • Assuming push adds all items ignoring full check
  • Thinking array grows beyond maxSize
  • Confusing top index with array length
4. This TypeScript stack code tries to check if the stack is empty:
class Stack {
  top = -1;
  isEmpty() {
    return this.top === 0;
  }
}

What is wrong with this isEmpty() method?
medium
A. It should check if top is 1, not 0
B. It should check if top is less than 0
C. It should check if top is greater than 0
D. It should check if top is -1, not 0

Solution

  1. Step 1: Understand top index for empty stack

    In stack implementation, top is -1 when stack is empty (no elements pushed yet).
  2. Step 2: Identify incorrect condition

    The code checks top === 0, which means one element is present, so it wrongly returns true for non-empty stack.
  3. Final Answer:

    It should check if top is -1, not 0 -> Option D
  4. Quick Check:

    Empty stack means top = -1 [OK]
Hint: Empty stack top is -1, not zero [OK]
Common Mistakes:
  • Checking top === 0 means stack has one element
  • Using greater than or less than instead of equality
  • Confusing empty with single element condition
5. You have a stack with maxSize 5 and current top at 3. You want to write a method canPush() that returns true only if the stack is not full and not empty. Which condition correctly implements this?
hard
A. return this.top > -1 && this.top < this.maxSize - 1;
B. return this.top >= 0 && this.top <= this.maxSize - 1;
C. return this.top !== -1 && this.top !== this.maxSize;
D. return this.top > 0 && this.top < this.maxSize;

Solution

  1. Step 1: Define conditions for not empty and not full

    Not empty means top > -1 (at least one element). Not full means top < maxSize - 1 (space to add more).
  2. Step 2: Combine conditions logically

    Both conditions must be true, so use logical AND: top > -1 && top < maxSize - 1.
  3. Final Answer:

    return this.top > -1 && this.top < this.maxSize - 1; -> Option A
  4. Quick Check:

    Not empty and not full = top > -1 and top < maxSize - 1 [OK]
Hint: Check top between -1 and maxSize-1 exclusive [OK]
Common Mistakes:
  • Using >= or <= incorrectly allowing empty or full
  • Using !== which can fail for boundary values
  • Confusing maxSize with maxSize - 1
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