Interview Questions on Stack (C Language)

📘 Basic Stack Concepts – Short Answer Interview Questions

  1. What is a stack in data structures?
    A linear data structure that follows LIFO (Last In First Out) principle.
  2. What are the basic operations of a stack?
    Push (add), Pop (remove), Peek (view top), IsEmpty, IsFull.
  3. How is a stack different from a queue?
    Stack is LIFO, queue is FIFO (First In First Out).
  4. What are real-world examples of stack behavior?
    Stack of plates, browser back button, function call stack.
  5. How can you implement a stack in C?
    Using arrays or linked lists.
  6. What is stack overflow?
    When you try to push an element onto a full stack.
  7. What is stack underflow?
    When you try to pop an element from an empty stack.
  8. What is the time complexity of stack operations?
    O(1) for push, pop, peek when properly implemented.
  9. How does the call stack work in program execution?
    Stores function calls, local variables, and return addresses.
  10. What is a stack frame?
    Memory block containing function parameters, local variables, and return address.
  11. Why is recursion implemented using stacks?
    Each recursive call pushes a new stack frame until base case is reached.
  12. What is the difference between static and dynamic stack implementation?
    Static has fixed size (array), dynamic can grow (linked list).
  13. How do you check for balanced parentheses using a stack?
    Push opening brackets, pop when matching closing bracket appears.
  14. What is postfix notation?
    Operator follows operands (e.g., AB+ instead of A+B).
  15. How can a stack be used to evaluate postfix expressions?
    Push operands, when operator comes, pop operands, compute, push result.
  16. What is the advantage of using linked list for stack implementation?
    No size limitation (until memory exhausted) and efficient memory usage.
  17. What is the disadvantage of array-based stack implementation?
    Fixed size, potential wasted space or overflow.
  18. How can two stacks be implemented in a single array?
    One stack grows from start, other from end of array.
  19. What is a stack pointer?
    Variable that tracks the top position in the stack.
  20. How does stack memory allocation work?
    Memory is allocated contiguously and managed via stack pointer.

📗 Advanced Stack Concepts – Short Answer Interview Questions

  1. What is the difference between stack and heap memory?
    Stack: automatic, fast, fixed size. Heap: manual, slower, dynamic size.
  2. How can a stack be used to implement a queue?
    Use two stacks (one for enqueue, one for dequeue operations).
  3. What is a monotonic stack?
    Stack where elements are in increasing or decreasing order.
  4. How can stacks be used in graph algorithms?
    Depth-First Search (DFS) uses stack to track vertices.
  5. What is the call stack in recursion?
    Stack that stores information about active subroutines/functions.
  6. How does stack memory management work in C?
    Compiler automatically manages stack frames for function calls.
  7. What is stack unwinding?
    Process of removing stack frames during exception handling.
  8. How can you find the minimum element in a stack in O(1) time?
    Use auxiliary stack to track minimums.
  9. What is a stack canary?
    Security value placed on stack to detect buffer overflows.
  10. How can stacks be used in backtracking algorithms?
    To store and retrieve previous states when exploring solutions.
  11. What is the relationship between stacks and tail recursion?
    Tail recursion can be optimized to use constant stack space.
  12. How can you reverse a stack using only stack operations?
    Use temporary stacks or recursion to reverse elements.
  13. What is the maximum depth of recursion in C?
    Limited by stack size; depends on compiler and system.
  14. How does stack allocation compare to heap allocation in performance?
    Stack allocation is faster but has size limitations.
  15. What is a stack overflow error in recursion?
    When recursion depth exceeds stack capacity.
  16. How can you implement multiple stacks efficiently?
    Single array with multiple stack pointers or linked lists.
  17. What are some common applications of stacks?
    Expression evaluation, syntax parsing, backtracking, memory management.
  18. How does the system stack work during function calls?
    Pushes return address, parameters, local variables; pops when returning.
  19. What is the difference between user stack and kernel stack?
    User stack for user mode, kernel stack for kernel mode operations.
  20. How can you detect stack growth direction in C?
    Compare addresses of local variables in nested function calls.