Data Structure Stack Practice Questions with Solutions

Introductions

A stack is a linear data structure that follows the LIFO (Last In, First Out) principle. These practice questions focus on using stacks rather than only learning their definition. You will practice push, pop, peek, checking whether a stack is empty, searching, finding the maximum value, reversing elements, and implementing a stack using JavaScript arrays. Each example is explained step by step for beginners. Data Structure Stack Practice questions with solutions help to understand the concepts.

Question 1: Push Elements into a Stack

Question

Create an empty stack and add the values 10, 20, and 30 using the push() operation. Print the stack.

Solution

A stack follows LIFO, which means the element added last stays at the top.

Start with an empty stack:

let stack = [];

Add 10:

stack.push(10);

Stack:

10

Add 20:

stack.push(20);

Stack:

20 ← Top
10

Add 30:

stack.push(30);

Now:

30 ← Top
20
10

Complete code:

let stack = [];

stack.push(10);
stack.push(20);
stack.push(30);

console.log(stack);

Output

[10, 20, 30]

Answer

The final stack contains:

Top → 30
      20
      10

The last inserted element, 30, is at the top.


Question 2: Pop an Element from a Stack

Question

Given this stack:

Top → 40
       30
       20
       10

Remove the top element and print the remaining stack.

Solution

The pop() operation removes the top element.

Create the stack:

let stack = [10, 20, 30, 40];

Remove the top element:

let removed = stack.pop();

The removed element is 40.

The stack becomes:

Top → 30
       20
       10

Complete code:

let stack = [10, 20, 30, 40];

let removed = stack.pop();

console.log("Removed:", removed);
console.log("Stack:", stack);

Output

Removed: 40
Stack: [10, 20, 30]

Answer

40 is removed because it was the top element.


Question 3: Find the Top Element Using Peek

Question

Find the top element of this stack without removing it:

Top → 50
       40
       30
       20
       10

Solution

In JavaScript, an array does not have a built-in peek() method, but we can access the last element using:

stack[stack.length - 1]

Create the stack:

let stack = [10, 20, 30, 40, 50];

Find the top:

let top = stack[stack.length - 1];

Print it:

console.log(top);

Complete code:

let stack = [10, 20, 30, 40, 50];

let top = stack[stack.length - 1];

console.log(top);

Output

50

Answer

The top element is 50.

The important point is that 50 is only viewed; it is not removed.


Question 4: Check Whether a Stack is Empty

Question

Check whether the following stack is empty:

[]

Print "Stack is Empty" if there are no elements.

Solution

A JavaScript array is empty when:

stack.length === 0

Code:

let stack = [];

if (stack.length === 0) {
    console.log("Stack is Empty");
} else {
    console.log("Stack is Not Empty");
}

Because the stack contains zero elements:

length = 0

the condition is true.

Output

Stack is Empty

Answer

The stack is empty.


Question 5: Pop All Elements from a Stack

Question

Remove and print all elements from this stack:

Top → 40
       30
       20
       10

Solution

A stack follows LIFO, so elements will be removed in this order:

40 → 30 → 20 → 10

We can continue calling pop() while the stack is not empty.

let stack = [10, 20, 30, 40];

while (stack.length > 0) {
    console.log(stack.pop());
}

Let’s see what happens:

Initial: [10, 20, 30, 40]

pop() → 40
Stack: [10, 20, 30]

pop() → 30
Stack: [10, 20]

pop() → 20
Stack: [10]

pop() → 10
Stack: []

Output

40
30
20
10

Answer

The elements are removed in reverse order of insertion because the stack follows LIFO.


Question 6: Search for an Element in a Stack

Question

Search for the value 30 in this stack:

Top → 50
       40
       30
       20
       10

Print "Found" if the value exists.

Solution

We can search through the array using a loop.

let stack = [10, 20, 30, 40, 50];

let target = 30;
let found = false;

for (let i = 0; i < stack.length; i++) {
    if (stack[i] === target) {
        found = true;
        break;
    }
}

if (found) {
    console.log("Found");
} else {
    console.log("Not Found");
}

The search checks:

10 → Not Found
20 → Not Found
30 → Found

Output

Found

Answer

The value 30 exists in the stack.


Question 7: Find the Maximum Element in a Stack

Question

Find the largest element in this stack:

Top → 25
       70
       15
       40
       10

Solution

Create the stack:

let stack = [10, 40, 15, 70, 25];

Initially assume the first element is the maximum:

let max = stack[0];

Now compare every element with max.

for (let i = 1; i < stack.length; i++) {
    if (stack[i] > max) {
        max = stack[i];
    }
}

The comparisons are:

10 → max = 10
40 → max = 40
15 → max = 40
70 → max = 70
25 → max = 70

Complete code:

let stack = [10, 40, 15, 70, 25];

let max = stack[0];

for (let i = 1; i < stack.length; i++) {
    if (stack[i] > max) {
        max = stack[i];
    }
}

console.log(max);

Output

70

Answer

The maximum element is 70.


Question 8: Reverse a Stack

Question

Reverse the following stack:

[10, 20, 30, 40, 50]

The expected result is:

[50, 40, 30, 20, 10]

Solution

We can use another stack to reverse the elements.

Original stack:

Bottom → 10
         20
         30
         40
Top    → 50

Create another empty stack:

let reversedStack = [];

Remove elements from the original stack and put them into the new stack:

while (stack.length > 0) {
    reversedStack.push(stack.pop());
}

Complete code:

let stack = [10, 20, 30, 40, 50];
let reversedStack = [];

while (stack.length > 0) {
    reversedStack.push(stack.pop());
}

console.log(reversedStack);

The process is:

Original stack:
[10, 20, 30, 40, 50]

pop 50 → reversedStack = [50]
pop 40 → reversedStack = [50, 40]
pop 30 → reversedStack = [50, 40, 30]
pop 20 → reversedStack = [50, 40, 30, 20]
pop 10 → reversedStack = [50, 40, 30, 20, 10]

Output

[50, 40, 30, 20, 10]

Answer

The stack is successfully reversed.


Question 9: Implement a Stack Using a JavaScript Class

Question

Create a Stack class with the following operations:

  • push()
  • pop()
  • peek()
  • isEmpty()

Test all four operations.

Solution

Create a class:

class Stack {
    constructor() {
        this.items = [];
    }
}

Add Push Operation

push(value) {
    this.items.push(value);
}

Add Pop Operation

pop() {
    return this.items.pop();
}

Add Peek Operation

peek() {
    return this.items[this.items.length - 1];
}

Add IsEmpty Operation

isEmpty() {
    return this.items.length === 0;
}

The complete class is:

class Stack {
    constructor() {
        this.items = [];
    }

    push(value) {
        this.items.push(value);
    }

    pop() {
        return this.items.pop();
    }

    peek() {
        return this.items[this.items.length - 1];
    }

    isEmpty() {
        return this.items.length === 0;
    }
}

let stack = new Stack();

stack.push(10);
stack.push(20);
stack.push(30);

console.log("Top:", stack.peek());

console.log("Removed:", stack.pop());

console.log("Top:", stack.peek());

console.log("Is Empty:", stack.isEmpty());

Let’s understand the operations:

push(10)
[10]

push(20)
[10, 20]

push(30)
[10, 20, 30]

peek()
30

pop()
30 removed

Stack:
[10, 20]

Output

Top: 30
Removed: 30
Top: 20
Is Empty: false

Answer

The Stack class successfully implements the basic stack operations.


Question 10: Check Balanced Parentheses Using a Stack

Question

Use a stack to check whether the following expression has balanced parentheses:

(a + b) * (c + d)

Print "Balanced" if every opening parenthesis has a matching closing parenthesis.

Solution

We use a stack to store opening parentheses.

When we see:

(

we push it into the stack.

When we see:

)

we remove one opening parenthesis using pop().

For this expression:

(a + b) * (c + d)

The process is:

(
→ push

)
→ pop

(
→ push

)
→ pop

At the end, the stack is empty.

Code:

let expression = "(a + b) * (c + d)";
let stack = [];
let balanced = true;

for (let char of expression) {

    if (char === "(") {
        stack.push(char);
    }

    if (char === ")") {
        if (stack.length === 0) {
            balanced = false;
            break;
        }

        stack.pop();
    }
}

if (stack.length !== 0) {
    balanced = false;
}

if (balanced) {
    console.log("Balanced");
} else {
    console.log("Not Balanced");
}

Let’s understand the important part:

if (char === "(") {
    stack.push(char);
}

An opening parenthesis is stored.

For a closing parenthesis:

if (char === ")") {
    stack.pop();
}

The matching opening parenthesis is removed.

At the end:

stack.length === 0

means all parentheses were matched.

Output

Balanced

Answer

The expression contains balanced parentheses.

Key Takeaways

  • A stack is a linear data structure based on LIFO (Last In, First Out).
  • The push() operation adds an element to the top.
  • The pop() operation removes the top element.
  • The peek() operation checks the top element without removing it.
  • An empty stack contains no elements.
  • JavaScript arrays can be used to implement basic stack operations.
  • Searching a stack generally takes O(n) time.
  • Finding the maximum element requires checking the elements and takes O(n) time.
  • Popping all elements from a stack takes O(n) time.
  • Stack operations such as push and pop at the top are generally O(1) with a JavaScript array.
  • Stacks are commonly used for parentheses matching, undo operations, function calls, expression evaluation, and backtracking.
  • Understanding LIFO is the key concept for solving stack problems.

FAQs

1. What is a stack in data structures?

A stack is a linear data structure that follows the LIFO (Last In, First Out) principle. The element added last is removed first.

2. What is the push operation in a stack?

push() adds a new element to the top of the stack.

3. What is the pop operation in a stack?

pop() removes and returns the element currently at the top of the stack.

4. What is the peek operation?

Peek returns or checks the top element without removing it from the stack.

5. What happens when we pop an empty stack?

There is no element available to remove. With a JavaScript array, calling pop() on an empty array returns undefined.

6. What is the time complexity of push and pop?

When implemented using a suitable stack structure, push and pop are generally O(1) operations because they work at the top of the stack.

7. Where are stacks used in programming?

Stacks are used in browser history, undo/redo operations, function calls, expression evaluation, parentheses matching, recursion, and backtracking algorithms.

Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.

Scroll to Top