Interview Questions on Stack (C Language)
📘 Basic Stack Concepts – Short Answer Interview Questions
-
What is a stack in data structures?
A linear data structure that follows LIFO (Last In First Out) principle.
-
What are the basic operations of a stack?
Push (add), Pop (remove), Peek (view top), IsEmpty, IsFull.
-
How is a stack different from a queue?
Stack is LIFO, queue is FIFO (First In First Out).
-
What are real-world examples of stack behavior?
Stack of plates, browser back button, function call stack.
-
How can you implement a stack in C?
Using arrays or linked lists.
-
What is stack overflow?
When you try to push an element onto a full stack.
-
What is stack underflow?
When you try to pop an element from an empty stack.
-
What is the time complexity of stack operations?
O(1) for push, pop, peek when properly implemented.
-
How does the call stack work in program execution?
Stores function calls, local variables, and return addresses.
-
What is a stack frame?
Memory block containing function parameters, local variables, and return address.
-
Why is recursion implemented using stacks?
Each recursive call pushes a new stack frame until base case is reached.
-
What is the difference between static and dynamic stack implementation?
Static has fixed size (array), dynamic can grow (linked list).
-
How do you check for balanced parentheses using a stack?
Push opening brackets, pop when matching closing bracket appears.
-
What is postfix notation?
Operator follows operands (e.g., AB+ instead of A+B).
-
How can a stack be used to evaluate postfix expressions?
Push operands, when operator comes, pop operands, compute, push result.
-
What is the advantage of using linked list for stack implementation?
No size limitation (until memory exhausted) and efficient memory usage.
-
What is the disadvantage of array-based stack implementation?
Fixed size, potential wasted space or overflow.
-
How can two stacks be implemented in a single array?
One stack grows from start, other from end of array.
-
What is a stack pointer?
Variable that tracks the top position in the stack.
-
How does stack memory allocation work?
Memory is allocated contiguously and managed via stack pointer.
📗 Advanced Stack Concepts – Short Answer Interview Questions
-
What is the difference between stack and heap memory?
Stack: automatic, fast, fixed size. Heap: manual, slower, dynamic size.
-
How can a stack be used to implement a queue?
Use two stacks (one for enqueue, one for dequeue operations).
-
What is a monotonic stack?
Stack where elements are in increasing or decreasing order.
-
How can stacks be used in graph algorithms?
Depth-First Search (DFS) uses stack to track vertices.
-
What is the call stack in recursion?
Stack that stores information about active subroutines/functions.
-
How does stack memory management work in C?
Compiler automatically manages stack frames for function calls.
-
What is stack unwinding?
Process of removing stack frames during exception handling.
-
How can you find the minimum element in a stack in O(1) time?
Use auxiliary stack to track minimums.
-
What is a stack canary?
Security value placed on stack to detect buffer overflows.
-
How can stacks be used in backtracking algorithms?
To store and retrieve previous states when exploring solutions.
-
What is the relationship between stacks and tail recursion?
Tail recursion can be optimized to use constant stack space.
-
How can you reverse a stack using only stack operations?
Use temporary stacks or recursion to reverse elements.
-
What is the maximum depth of recursion in C?
Limited by stack size; depends on compiler and system.
-
How does stack allocation compare to heap allocation in performance?
Stack allocation is faster but has size limitations.
-
What is a stack overflow error in recursion?
When recursion depth exceeds stack capacity.
-
How can you implement multiple stacks efficiently?
Single array with multiple stack pointers or linked lists.
-
What are some common applications of stacks?
Expression evaluation, syntax parsing, backtracking, memory management.
-
How does the system stack work during function calls?
Pushes return address, parameters, local variables; pops when returning.
-
What is the difference between user stack and kernel stack?
User stack for user mode, kernel stack for kernel mode operations.
-
How can you detect stack growth direction in C?
Compare addresses of local variables in nested function calls.
Related Data Structure Resources