Commonly Asked Data Structure Interview Questions on Stack
Last Updated :
23 Jul, 2025
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
Similar Reads
Basics & Prerequisites
Data Structures
Getting Started with Array Data StructureArray is a collection of items of the same variable type that are stored at contiguous memory locations. It is one of the most popular and simple data structures used in programming. Basic terminologies of ArrayArray Index: In an array, elements are identified by their indexes. Array index starts fr
14 min read
String in Data StructureA string is a sequence of characters. The following facts make string an interesting data structure.Small set of elements. Unlike normal array, strings typically have smaller set of items. For example, lowercase English alphabet has only 26 characters. ASCII has only 256 characters.Strings are immut
2 min read
Hashing in Data StructureHashing is a technique used in data structures that efficiently stores and retrieves data in a way that allows for quick access. Hashing involves mapping data to a specific index in a hash table (an array of items) using a hash function. It enables fast retrieval of information based on its key. The
2 min read
Linked List Data StructureA linked list is a fundamental data structure in computer science. It mainly allows efficient insertion and deletion operations compared to arrays. Like arrays, it is also used to implement other data structures like stack, queue and deque. Hereâs the comparison of Linked List vs Arrays Linked List:
2 min read
Stack Data StructureA Stack is a linear data structure that follows a particular order in which the operations are performed. The order may be LIFO(Last In First Out) or FILO(First In Last Out). LIFO implies that the element that is inserted last, comes out first and FILO implies that the element that is inserted first
2 min read
Queue Data StructureA Queue Data Structure is a fundamental concept in computer science used for storing and managing data in a specific order. It follows the principle of "First in, First out" (FIFO), where the first element added to the queue is the first one to be removed. It is used as a buffer in computer systems
2 min read
Tree Data StructureTree Data Structure is a non-linear data structure in which a collection of elements known as nodes are connected to each other via edges such that there exists exactly one path between any two nodes. Types of TreeBinary Tree : Every node has at most two childrenTernary Tree : Every node has at most
4 min read
Graph Data StructureGraph Data Structure is a collection of nodes connected by edges. It's used to represent relationships between different entities. If you are looking for topic-wise list of problems on different topics like DFS, BFS, Topological Sort, Shortest Path, etc., please refer to Graph Algorithms. Basics of
3 min read
Trie Data StructureThe Trie data structure is a tree-like structure used for storing a dynamic set of strings. It allows for efficient retrieval and storage of keys, making it highly effective in handling large datasets. Trie supports operations such as insertion, search, deletion of keys, and prefix searches. In this
15+ min read
Algorithms
Searching AlgorithmsSearching algorithms are essential tools in computer science used to locate specific items within a collection of data. In this tutorial, we are mainly going to focus upon searching in an array. When we search an item in an array, there are two most common algorithms used based on the type of input
2 min read
Sorting AlgorithmsA Sorting Algorithm is used to rearrange a given array or list of elements in an order. For example, a given array [10, 20, 5, 2] becomes [2, 5, 10, 20] after sorting in increasing order and becomes [20, 10, 5, 2] after sorting in decreasing order. There exist different sorting algorithms for differ
3 min read
Introduction to RecursionThe process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called a recursive function. A recursive algorithm takes one step toward solution and then recursively call itself to further move. The algorithm stops once we reach the solution
14 min read
Greedy AlgorithmsGreedy algorithms are a class of algorithms that make locally optimal choices at each step with the hope of finding a global optimum solution. At every step of the algorithm, we make a choice that looks the best at the moment. To make the choice, we sometimes sort the array so that we can always get
3 min read
Graph AlgorithmsGraph is a non-linear data structure like tree data structure. The limitation of tree is, it can only represent hierarchical data. For situations where nodes or vertices are randomly connected with each other other, we use Graph. Example situations where we use graph data structure are, a social net
3 min read
Dynamic Programming or DPDynamic Programming is an algorithmic technique with the following properties.It is mainly an optimization over plain recursion. Wherever we see a recursive solution that has repeated calls for the same inputs, we can optimize it using Dynamic Programming. The idea is to simply store the results of
3 min read
Bitwise AlgorithmsBitwise algorithms in Data Structures and Algorithms (DSA) involve manipulating individual bits of binary representations of numbers to perform operations efficiently. These algorithms utilize bitwise operators like AND, OR, XOR, NOT, Left Shift, and Right Shift.BasicsIntroduction to Bitwise Algorit
4 min read
Advanced
Segment TreeSegment Tree is a data structure that allows efficient querying and updating of intervals or segments of an array. It is particularly useful for problems involving range queries, such as finding the sum, minimum, maximum, or any other operation over a specific range of elements in an array. The tree
3 min read
Pattern SearchingPattern searching algorithms are essential tools in computer science and data processing. These algorithms are designed to efficiently find a particular pattern within a larger set of data. Patten SearchingImportant Pattern Searching Algorithms:Naive String Matching : A Simple Algorithm that works i
2 min read
GeometryGeometry is a branch of mathematics that studies the properties, measurements, and relationships of points, lines, angles, surfaces, and solids. From basic lines and angles to complex structures, it helps us understand the world around us.Geometry for Students and BeginnersThis section covers key br
2 min read
Interview Preparation
Practice Problem