stack data structure: 17 flashcards
All 17 flashcards from this kit, free and with no sign-up.
1. What does LIFO stand for in the context of a stack?
Show the answer
Last In, First Out — the last element added to the stack is the first one removed.
2. List the two primary operations of a stack and their time complexities.
Show the answer
Push (add an element to the top) and Pop (remove the top element). Both are O(1) in typical implementations.
3. What is the purpose of the `peek` (or `top`) operation?
Show the answer
It returns the top element without removing it, allowing inspection of the stack's top.
4. In a stack, which element is removed by a `pop` operation?
Show the answer
The element that was most recently pushed (the top element).
5. What does the `isEmpty` operation check?
Show the answer
It returns true if the stack has no elements, false otherwise.
6. What is stack overflow?
Show the answer
An attempt to push an element onto a stack that has reached its maximum capacity (in fixed-size implementations).
7. What is stack underflow?
Show the answer
An attempt to pop an element from an empty stack.
8. Name three common applications of stacks.
Show the answer
Function call management (call stack), expression evaluation (e.g., converting infix to postfix), and undo/redo functionality in editors.
9. How does a stack help in checking balanced parentheses in an expression?
Show the answer
Scan the expression: push each opening bracket; when a closing bracket is found, pop the stack and check if it matches the corresponding opening bracket. If the stack is empty or mismatched, the expression is unbalanced.
10. Given a stack with elements [bottom] 1, 2, 3 [top], what is the result of a `pop` operation?
Show the answer
The element 3 is removed, leaving [bottom] 1, 2 [top].
11. After pushing 5, then 10, then 15 onto a stack, what sequence would a series of `pop` operations return?
Show the answer
15, then 10, then 5 (LIFO order).
12. What is the time complexity of searching for a specific element in a stack (without using auxiliary structures)?
Show the answer
O(n) in the worst case, because you may need to pop elements until you find it (and then restore them).
13. How can a stack be used to reverse a string?
Show the answer
Push each character of the string onto the stack, then pop all characters; the popped sequence is the reversed string.
14. In the context of function calls, what does the call stack store?
Show the answer
Information about active function calls, such as return addresses, local variables, and parameters, to manage execution and return flow.
15. What is the difference between a stack implemented with an array and one with a linked list?
Show the answer
Array-based stacks have a fixed capacity (may overflow) but are cache-friendly; linked-list stacks can grow dynamically but use extra memory for pointers.
16. How would you implement a stack using two queues?
Show the answer
Push: enqueue to queue1. Pop: move all elements except the last from queue1 to queue2, dequeue the last element (the top), then swap the names of queue1 and queue2.
17. What is the role of a stack in evaluating postfix (Reverse Polish) expressions?
Show the answer
Scan the expression: push operands; when an operator is encountered, pop two operands, apply the operator, and push the result. The final value is the last element on the stack.
Your notes can be a kit like these
Every kit here was made from real study material. Point Veda at a chapter, a PDF, a photo or a link and get notes, flashcards, practice MCQs, games and a scored test — free, in 42 languages.
- Chapter notes
- Flashcards
- Practice MCQs
- Scored tests
- Exam prep
Get Veda free See what students get
Free to start · No ads · Works in 42 languages