Bird
0
0

Given this Kafka event stream, what will the consumer state be after processing all events?

medium📝 Predict Output Q13 of 15
Kafka - Event-Driven Architecture
Given this Kafka event stream, what will the consumer state be after processing all events?
events = [
  {"type": "create", "id": 1, "value": 10},
  {"type": "update", "id": 1, "value": 20},
  {"type": "create", "id": 2, "value": 5},
  {"type": "delete", "id": 1}
]

state = {}
for event in events:
    if event["type"] == "create" or event["type"] == "update":
        state[event["id"]] = event["value"]
    elif event["type"] == "delete":
        state.pop(event["id"], None)

print(state)
A{}
B{"1": 20, "2": 5}
C{"1": 10, "2": 5}
D{"2": 5}
Step-by-Step Solution
Solution:
  1. Step 1: Process each event in order

    First create id 1 with value 10, then update id 1 to 20, create id 2 with 5, then delete id 1.
  2. Step 2: Determine final state

    After deletion, id 1 is removed, only id 2 with value 5 remains.
  3. Final Answer:

    {"2": 5} -> Option D
  4. Quick Check:

    Delete removes id 1, only id 2 left [OK]
Quick Trick: Track events step-by-step to update state correctly [OK]
Common Mistakes:
MISTAKES
  • Forgetting the delete event removes id 1
  • Confusing update with create
  • Assuming old values remain after update

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kafka Quizzes