Stack & Queue — Python Technical Interview Questions & Answers
Stack & Queue
Stack & Queue
- Stack = LIFO. Use a list: append + pop(). Queue = FIFO. Use collections.deque: append + popleft() (list pop(0) is slow).
- Board: stack [1,2,3] pop → 3. Queue Asha,Ravi popleft → Asha first.
- Stack & Queue — output: stack pop B (LIFO) then queue Asha (FIFO).
Technical Interview Tip: LIFO vs FIFO + which Python type.
Code Examples for Stack & Queue
Example
# Stack & Queue
from collections import deque
stack = []
stack.append("A")
stack.append("B")
print("stack pop", stack.pop())
q = deque(["Asha", "Ravi"])
print("queue", q.popleft())
Stack & Queue — output: stack pop B (LIFO) then queue Asha (FIFO).
How to Explain Stack & Queue to an Interviewer
Explain Stack & Queue as if you are teaching a junior — definition, then one tiny script.
Stack LIFO : append/pop Queue FIFO : deque popleft
What does the example print, and why?
Stack & Queue — output: stack pop B (LIFO) then queue Asha (FIFO)
What mistake do freshers make with Stack & Queue?
list.pop(0) is O(n)