Bird
Raised Fist0

Consider this simplified event-driven code snippet:

medium๐Ÿ“ Analysis Q13 of Q15
LLD - Advanced LLD Concepts
Consider this simplified event-driven code snippet:
event_queue = []

def produce(event):
    event_queue.append(event)

def consume():
    if event_queue:
        return event_queue.pop(0)
    return None

produce('A')
produce('B')
print(consume())
print(consume())
print(consume())

What is the output?
ANone None None
BB A None
CA B None
DA None B
Step-by-Step Solution
Solution:
  1. Step 1: Trace event production

    Two events 'A' and 'B' are added to the queue in order: ['A', 'B'].
  2. Step 2: Trace event consumption

    consume() removes and returns the first event: first 'A', then 'B', then None when empty.
  3. Final Answer:

    A B None -> Option C
  4. Quick Check:

    FIFO queue returns A then B then None [OK]
Quick Trick: Queue pops first-in event first (FIFO) [OK]
Common Mistakes:
MISTAKES
  • Assuming LIFO instead of FIFO
  • Forgetting to check empty queue
  • Mixing order of events

Want More Practice?

15+ quiz questions ยท All difficulty levels ยท Free

Free Signup - Practice All Questions
More LLD Quizzes