Open In App

Commonly Asked Data Structure Interview Questions on Stack

Last Updated : 23 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

A stack follows the Last In, First Out (LIFO) principle, meaning the last element added is the first to be removed. Stacks are a fundamental data structure used in many real-world applications, including expression evaluation, function call management, and backtracking algorithms.

Theoretical Questions for Interviews on Stack

1. What is a stack?

A stack is a linear data structure that follows the Last-In-First-Out (LIFO) principle.

2. What are the operations performed on a stack?

The common operations on a stack are push (insert an element), pop (remove the top element), and peek (view the top element).

3. How is a stack implemented in an array?

A stack can be implemented using an array by maintaining a pointer to the top of the stack.

4. What is the time complexity of stack operations?

Push, pop, and peek operations have a time complexity of O(1). Please remember in case of linked list implementation and a fixed sized array implementation, the time complexity in the worst case is O(1), However if we use dynamic sized array and allow the stack to grow, then the amortized time complexity [Average over n operations] is O(1).

5. What is the time complexity of inserting an element at the bottom of a stack?

Insert at Bottom: The time complexity of inserting an element at the bottom of a stack is O(n), as all the elements need to be popped off and pushed back onto the stack.

6. What are the applications of a stack?

Stacks are used in various applications, such as function calls, recursion, expression evaluation, and parsing.

7. What is a stack overflow?

A stack overflow occurs when the stack exceeds its allocated memory.

8. What is a stack underflow?

A stack underflow occurs when the stack is empty and an attempt is made to pop an element.

9. How can you implement a stack using two queues?

The idea is to use one queue for normal insertion (push operation) and the second queue for reversal of elements to mimic the stack's pop operation. The goal is to make the pop operation efficient by adjusting the order of elements between the two queues.

10. What is a postfix expression, and how can a stack be used to evaluate it?

A postfix expression is an expression where the operator follows the operands. To evaluate a postfix expression using a stack, you push operands onto the stack and, when encountering an operator, pop the required operands from the stack, perform the operation, and push the result back onto the stack.

11. What is a prefix expression, and how can a stack be used to evaluate it?

A prefix expression is an expression where the operator precedes the operands. To evaluate a prefix expression using a stack, you traverse the expression from right to left, pushing operands onto the stack. When an operator is encountered, you pop the required operands from the stack, perform the operation, and push the result back onto the stack.

12. How can a stack be implemented using a linked list?

A stack can be implemented using a linked list by using the head of the list as the top of the stack. In this implementation:

  • Push: Add a new node at the head of the linked list (O(1) time).
  • Pop: Remove the node from the head (O(1) time).
  • Peek: Access the data of the node at the head (O(1) time).

13. What is the time complexity of converting an infix expression to postfix?

To convert an infix expression to postfix using a stack, we use an algorithm that processes each character of the expression, applying operator precedence rules and using the stack to hold operators temporarily.

Time Complexity: O(n), where n is the length of the infix expression. Each character is processed once, and each operation (push or pop) takes constant time.

14. What is the time complexity of converting an infix expression to prefix?

To convert an infix expression to a prefix expression, we use a similar algorithm as for postfix conversion but traverse the expression from right to left.

Time Complexity: O(n), where n is the length of the infix expression. Reversing the expression takes O(n), converting it to postfix takes O(n), and the final reverse also takes O(n).

15. How can a stack be used to check if an expression containing three types of brackets [, ( and { is balanced?

A stack can be used to check if a parenthesis expression is balanced by following these steps:

  • Push the opening parenthesis onto the stack.
  • When an closing parenthesis is encountered, pop the top element from the stack and check if it matches the closing parenthesis.
  • If the stack is empty at the end of the expression, then the expression is balanced.
  • If the stack is not empty, then the expression is not balanced.

Please note that if there is only one type of bracket, then we do not need stack. We can solve the problem using a count variable. However for multiples types of brackets a stack is needed to solve.

Top Coding Interview Questions on Stack

The following list of 50 stack coding problems covers a range of difficulty levels, from easy to hard, to help candidates prepare for interviews.

Top 50 Problems on Stack Data Structure asked in SDE Interviews


Next Article
Article Tags :
Practice Tags :

Similar Reads