Stacks Flashcards
(50 cards)
What is a stack?
A linear data structure that follows the Last In, First Out (LIFO) principle.
What is LIFO?
Last In, First Out — the most recently added item is the first to be removed.
What are the primary operations on a stack?
Push, pop, peek (or top), and isEmpty.
What does the push operation do?
Adds an item to the top of the stack.
What does the pop operation do?
Removes and returns the top item from the stack.
What does the peek (or top) operation do?
Returns the top item without removing it.
What does the isEmpty operation do?
Checks if the stack has no elements.
What is the time complexity of push and pop in a stack?
O(1)
How can a stack be implemented?
Using arrays or linked lists.
What is a stack overflow?
An error that occurs when trying to push onto a full stack (in a fixed-size implementation).
What is a stack underflow?
An error that occurs when trying to pop from an empty stack.
What is the space complexity of a stack with n elements?
O(n)
What is a real-world example of a stack?
Undo functionality in text editors.
What is the role of a stack in recursion?
Stores function calls, return addresses, and local variables.
How does a stack help evaluate expressions?
Used in parsing and evaluating postfix (Reverse Polish) notation.
How is a stack used in Depth-First Search (DFS)?
DFS uses a stack to keep track of vertices to visit.
What is the difference between peek and pop?
Peek returns the top element without removing it; pop removes and returns it.
What is the difference between a dynamic and static stack?
A dynamic stack grows as needed; a static stack has a fixed size.
What is a use case for stack in backtracking algorithms?
Stores previous states for undoing decisions.
What data structure is typically used to implement a call stack?
A stack.
Can a stack be implemented using a queue?
Yes, using two queues to simulate stack behavior.
Can a queue be implemented using a stack?
Yes, using two stacks to simulate queue behavior.
What is a minimum stack?
A stack that supports retrieving the minimum element in O(1) time.
How do you implement a minimum stack?
Use an auxiliary stack to track minimum values.