DATA STRUCTURE VIVA

1. What is a stack in data structures?
Sol :- A stack is a linear data structure that follows the LIFO (Last In, First Out) principle, where the last element added is the first to be removed.
2. Explain the principle of operation for a stack.
Sol :- The principle is LIFO, meaning the last item added to the stack is the first one to be removed.
3. What is LIFO, and how does it relate to stacks?
Sol :- LIFO stands for Last In, First Out. It describes how stacks work, where the most recently added item is accessed first.
4. Mention at least two real-life examples of stack operations.
SOl :- A pile of plates in a cafeteria.
The undo operation in text editors.
5. What are the two main operations performed on a stack? Explain them.
Sol :-
Push: Adds an item to the top of the stack.
Pop: Removes and returns the top item from the stack.
6. What is stack overflow, and when does it occur?
Sol :- Stack overflow occurs when you try to push an item into a stack that has reached its maximum capacity.
7. What is stack underflow, and when does it occur?
Sol :- Stack underflow occurs when you try to pop an item from an empty stack.
8. How is a stack implemented in Python?
Sol :- In Python, a stack can be implemented using a list or the collections.deque module.
9. What does the push() operation do in a stack?
Sol :- Adds an element to the top of the stack.
10. What does the pop() operation do in a stack?
Sol :- Removes and returns the top element of the stack.
11. What is the purpose of the peek() or top() operation in a stack?
Sol :- Returns the top element of the stack without removing it.
12. Explain how a stack is used in function call management.
Sol :- Function calls are stored in a stack, where the most recently called function is completed first before returning to previous calls.
13. How can stacks be used to reverse a string?
Sol :- Push all characters of the string into a stack and pop them one by one to get the reversed string.
14. What is the role of stacks in expression evaluation?
Sol :- Stacks help evaluate expressions by storing operators and operands in the correct sequence.
15. Explain how stacks are used to check for balanced parentheses in an expression.
Sol :- Push opening brackets onto the stack. For every closing bracket, check if the stack’s top matches. If it doesn’t or the stack is empty, the expression is unbalanced.

Leave a Reply

Your email address will not be published. Required fields are marked *

sign up!

We’ll send you the hottest deals straight to your inbox so you’re always in on the best-kept software secrets.