Bird
0
0

In this Kafka event sourcing code, the final state is incorrect:

medium📝 Debug Q7 of 15
Kafka - Event-Driven Architecture
In this Kafka event sourcing code, the final state is incorrect:
state = 0
for event in events:
    if event['type'] == 'withdrawal':
        state -= event['amount']
    else:
        state = event['amount']

What is the bug?
AMissing event type check for deposits
BElse resets state instead of adding deposits
CWithdrawal should add amount, not subtract
DState should be initialized to None
Step-by-Step Solution
Solution:
  1. Step 1: Analyze else block effect

    Else resets state to event amount, losing previous total.
  2. Step 2: Correct logic for deposits

    Else should add event amount to state, not reset it.
  3. Final Answer:

    Else resets state instead of adding deposits -> Option B
  4. Quick Check:

    Bug = state reset instead of accumulation [OK]
Quick Trick: Add deposits, don't reset state in else block [OK]
Common Mistakes:
MISTAKES
  • Confusing subtraction with addition
  • Ignoring else block logic
  • Incorrect state initialization

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kafka Quizzes