Stack Basics: 18 flashcards

All 18 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 what they do.

Show the answer

**push** adds an element to the top; **pop** removes the top element.

3. What is the time complexity of push and pop operations on a stack implemented with an array or linked list?

Show the answer

O(1) for both push and pop, assuming no resizing issues in array implementation.

4. What does the 'top' pointer/reference in a stack indicate?

Show the answer

It points to the most recently added element, which is the next candidate for pop.

5. What is the condition for a stack underflow?

Show the answer

Attempting to pop or peek from an empty stack.

6. What is the condition for a stack overflow?

Show the answer

Attempting to push onto a stack that has reached its maximum capacity (in fixed-size array implementation).

7. How does a stack help in checking balanced parentheses in an expression?

Show the answer

Push each opening bracket; on a closing bracket, pop and check if it matches the expected opening bracket. If mismatch or stack empty at the end, it's unbalanced.

8. In the context of function calls, what is the stack used for?

Show the answer

To manage function call frames (return addresses, local variables) — each call pushes a frame, and each return pops it, following LIFO order.

9. 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 — they come out in reverse order.

10. 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.

11. Name three real-world applications of a stack.

Show the answer

Undo/redo in editors, browser back button, and expression conversion/evaluation (e.g., infix to postfix).

12. What is the difference between a stack's 'peek' (or 'top') operation and 'pop'?

Show the answer

Peek returns the top element without removing it; pop removes and returns the top element.

13. In an array-based stack, what is the typical initial value of 'top' and what does it indicate?

Show the answer

Typically -1, indicating an empty stack. After a push, top becomes 0, then 1, etc.

14. How does a stack help in converting an infix expression to postfix?

Show the answer

Operators are pushed onto the stack and popped based on precedence and associativity, while operands are directly output.

15. What is the output of the following sequence: push(5), push(10), pop(), push(7), pop(), pop()?

Show the answer

The pops return 10, then 7, then 5. The final stack is empty.

16. Which data structure is used to implement depth-first search (DFS) in graph traversal?

Show the answer

A stack (either explicitly or via recursion's call stack).

17. What is the main drawback of using a fixed-size array to implement a stack?

Show the answer

It has a limited capacity, causing stack overflow if exceeded; dynamic resizing is needed to avoid this.

18. In a linked-list implementation of a stack, where is the top element typically placed?

Show the answer

At the head of the list, so push and pop operate at the head in O(1) time.

Make your own

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