Advanced Python Stack Questions Answers
Advanced Python Stack Questions Answers
1. How would you implement a stack using Python lists? What are the time complexities of push and
pop operations?
Answer: You can use Python lists where `append()` is push and `pop()` is pop. Both operations are O(1)
average case.
2. Implement a stack using two queues. What is the time complexity of each operation?
Answer: Use two `queue.Queue` objects. Push: O(n), Pop: O(1) or vice versa depending on the approach.
Answer: Use two stacks: one for enqueue, one for dequeue. Transfer elements as needed. Amortized O(1)
per operation.
4. Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.
Answer: Use two stacks: one for all elements, one for tracking minimums. Push/pop both stacks accordingly.
5. What are the trade-offs between using a list and a collections.deque for implementing a stack?
Answer: `list` is faster for append/pop at the end. `deque` is optimized for appends/pops at both ends and is
thread-safe.
Answer: Each recursive call pushes a frame on the call stack. Python manages it automatically. Example:
factorial function.
7. How would you detect and resolve stack overflow in a recursive Python function?
convert to iteration.
8. How do you evaluate a postfix (Reverse Polish Notation) expression using a stack in Python?
Answer: Iterate over tokens, push numbers, pop two for operations, push result back. Stack stores
intermediate results.
9. Write a Python function to check if a given string of brackets is balanced using a stack.
Answer: Use a stack to push opening brackets, pop when a matching closing bracket appears. Check stack
10. What is the use of inspect.stack() in debugging recursive functions or tracing execution?
Answer: It returns the current call stack. Useful for tracing function calls and debugging recursion issues.
11. Design a stack class in Python that also returns the maximum element in constant time.
Answer: Use an auxiliary stack to track max values. Push new max when needed, pop both stacks.
12. How can you use a stack to reverse a string or list in Python?
Answer: Push all characters/items onto a stack, then pop and append to new list/string.
Answer: Use one stack for back history, another for forward. Move pages between stacks on navigation.
Answer: Use a list to simulate function call stack: push function name on call, pop on return.
15. Describe how depth-first search (DFS) uses a stack, and implement DFS using a stack in Python.
Answer: DFS uses a stack to track the path. Use an explicit list stack to implement iterative DFS.
Answer: Each function call creates a frame on the stack with local variables, instruction pointer, and return
address.
17. Can you explain how Python's exception handling uses a stack?
Answer: Exceptions bubble up through the call stack until caught. The stack unwinds in the process.
18. Write a Python function to convert an infix expression to postfix using a stack.
Answer: Use a precedence dictionary, push operators to stack, output operands. Handle parentheses
carefully.
19. Implement a custom Stack class with dynamic resizing (similar to ArrayList behavior).
Answer: Python lists resize dynamically. Wrap list with push, pop, peek, and check size/capacity.
20. What is a monotonic stack? Write Python code to solve the 'Next Greater Element' problem.
Answer: Monotonic stack is increasing/decreasing stack. Use it to efficiently find next greater/smaller
elements.
21. How would you implement a stack with O(1) time for getMin() and getMax() operations?
Answer: Use two extra stacks for min and max values. Update them during push and pop.
22. Explain tail recursion optimization and why Python doesn't support it with respect to the call
stack.
Answer: Tail recursion optimization reuses the stack frame. Python doesn't do this to keep stack traces
23. What is the difference between recursion and using an explicit stack in Python?
Answer: Recursion uses implicit stack; explicit stack is managed by user code. Explicit avoids recursion
limits.
24. Create a Python decorator that logs stack depth of a function call using the sys module.
Answer: Use `sys._getframe()` to measure depth. Log info before calling the function inside decorator.
25. How do you use a stack to simulate backtracking in Python (e.g., N-Queens or Sudoku)?
Answer: Push states/choices onto the stack. Pop to backtrack when constraints are violated.