Stacks Flashcards

(50 cards)

1
Q

What is a stack?

A

A linear data structure that follows the Last In, First Out (LIFO) principle.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is LIFO?

A

Last In, First Out — the most recently added item is the first to be removed.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What are the primary operations on a stack?

A

Push, pop, peek (or top), and isEmpty.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What does the push operation do?

A

Adds an item to the top of the stack.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What does the pop operation do?

A

Removes and returns the top item from the stack.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What does the peek (or top) operation do?

A

Returns the top item without removing it.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What does the isEmpty operation do?

A

Checks if the stack has no elements.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the time complexity of push and pop in a stack?

A

O(1)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How can a stack be implemented?

A

Using arrays or linked lists.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is a stack overflow?

A

An error that occurs when trying to push onto a full stack (in a fixed-size implementation).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is a stack underflow?

A

An error that occurs when trying to pop from an empty stack.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is the space complexity of a stack with n elements?

A

O(n)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is a real-world example of a stack?

A

Undo functionality in text editors.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is the role of a stack in recursion?

A

Stores function calls, return addresses, and local variables.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How does a stack help evaluate expressions?

A

Used in parsing and evaluating postfix (Reverse Polish) notation.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How is a stack used in Depth-First Search (DFS)?

A

DFS uses a stack to keep track of vertices to visit.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

What is the difference between peek and pop?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

What is the difference between a dynamic and static stack?

A

A dynamic stack grows as needed; a static stack has a fixed size.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

What is a use case for stack in backtracking algorithms?

A

Stores previous states for undoing decisions.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

What data structure is typically used to implement a call stack?

A

A stack.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

Can a stack be implemented using a queue?

A

Yes, using two queues to simulate stack behavior.

22
Q

Can a queue be implemented using a stack?

A

Yes, using two stacks to simulate queue behavior.

23
Q

What is a minimum stack?

A

A stack that supports retrieving the minimum element in O(1) time.

24
Q

How do you implement a minimum stack?

A

Use an auxiliary stack to track minimum values.

25
How is memory managed in a stack-based implementation?
Each push allocates memory; each pop deallocates it (in linked list).
26
What is the role of the call stack in programming languages?
Tracks function calls, parameters, and local variables.
27
What is tail recursion in the context of the call stack?
A recursion where the recursive call is the last operation, enabling optimizations.
28
What causes a stack overflow in recursion?
Too many recursive calls without returning, exhausting stack memory.
29
What is the maximum depth of the call stack?
Depends on the system and language implementation.
30
How do you convert an infix expression to postfix using a stack?
Use the Shunting Yard algorithm developed by Edsger Dijkstra.
31
What is the Shunting Yard algorithm?
An algorithm that converts infix expressions to postfix using a stack.
32
How are function calls stored in memory?
Each function call is stored as a frame on the call stack.
33
What happens when a function returns?
Its frame is removed from the call stack.
34
What is a stack trace?
A report of active stack frames at a certain point during program execution, often shown during errors.
35
What are balanced parentheses problems?
Problems where a stack is used to ensure every opening symbol has a matching closing symbol.
36
What is a common algorithm that uses stacks for string processing?
Checking balanced parentheses, brackets, and braces.
37
How do you reverse a string using a stack?
Push all characters to a stack, then pop them to form the reversed string.
38
What is the time complexity of checking for balanced parentheses using a stack?
O(n)
39
Can a stack be used to simulate a queue?
Yes, by using two stacks: one for enqueue, one for dequeue.
40
What is a monotonic stack?
A stack that maintains its elements in a specific order (increasing or decreasing).
41
What is a use case for a monotonic stack?
Finding the next greater or smaller element for each item in an array.
42
What is a browser history example of a stack?
The back button uses a stack to navigate backward through visited pages.
43
What is a postfix expression?
An expression where the operator comes after the operands (e.g., 2 3 +).
44
How do you evaluate a postfix expression using a stack?
Scan tokens left to right, push operands, pop for operators.
45
What is an expression tree and how is it evaluated?
A binary tree representing an expression, evaluated using post-order traversal with a stack.
46
What is the difference between a stack and a queue?
Stack is LIFO; queue is FIFO.
47
What are sentinel values in stacks?
Special values used to mark boundaries or invalid states.
48
How do stacks support undo operations?
Each action is pushed to the stack; popping undoes the last action.
49
What is the space complexity of recursive function calls?
O(d), where d is the recursion depth.
50
What are stack frames?
Memory blocks storing function info during calls, such as parameters, return addresses, and local variables.