DSA Intreview Questions – Stack
1. [Asked in Wipro] What is a Stack in Data Structures?
Answer:
A stack is a linear data structure that follows the LIFO (Last In, First Out) principle. It means the last item inserted into the stack is the first one to be removed. Itโs similar to a stack of plates where you add and remove from the top only.
2. [Asked in Infosys] Name some common operations performed on a Stack.
Answer:
Common stack operations are:
- Push โ Add an element to the top.
- Pop โ Remove the top element.
- Peek or Top โ View the top element without removing it.
- isEmpty โ Check if the stack is empty.
3. [Asked in Amazon] What real-world scenarios use stacks?
Answer:
Stacks are used in various real-world applications, including:
- Undo functionality (Ctrl+Z) in text editors.
- Browser history (back button functionality).
- Expression evaluation and parsing (like evaluating mathematical expressions).
- Memory management (call stack in programming languages).
4. [Asked in Cognizant] Explain the difference between Stack and Queue.
Answer:
| Stack | Queue |
|---|---|
| Follows LIFO (Last In First Out). | Follows FIFO (First In First Out). |
| Elements added/removed from the top only. | Elements added at rear and removed from front. |
| Example: Undo operation. | Example: Customer waiting line. |
5. [Asked in Microsoft] What happens if you try to Pop an element from an empty stack?
Answer:
Trying to Pop an element from an empty stack results in an error or exception, commonly known as Stack Underflow. Always check if a stack is empty using isEmpty() before popping elements.
6. [Asked in IBM] What data structures can you use to implement a Stack?
Answer:
A Stack can be implemented using:
- Arrays (simple implementation)
- Linked Lists (dynamic size implementation)
Arrays have fixed size, whereas linked lists allow stacks to grow dynamically.
7. [Asked in TCS] Can you briefly explain what “Stack Overflow” means?
Answer:
Stack Overflow occurs when you try to add (Push) an element onto a stack that has reached its maximum capacity. This is common when using an array-based stack implementation, causing the stack to run out of space.
8. [Asked in Google] What is the “Peek” operation in a Stack?
Answer:
The Peek operation allows viewing the top element of the stack without removing it. This operation is useful for checking the stackโs current state without modifying its contents.
9. [Asked in Accenture] Is a Stack a linear or non-linear data structure? Why?
Answer:
A Stack is a linear data structure because elements are stored and accessed in sequential order, one after another, following a single straight path (top to bottom).
10. [Asked in Flipkart] Mention an advantage and disadvantage of using a Stack.
Answer:
- Advantage: Easy implementation of the LIFO approach, efficient insertion and removal operations at the top (O(1) complexity).
- Disadvantage: Limited flexibility, as you can access only the top element directly, making it less efficient if you need random access to elements.
