Bird
0
0

Find the logical error in this balance update pseudocode:

medium📝 Analysis Q6 of 15
LLD - Design — Splitwise (Expense Sharing)
Find the logical error in this balance update pseudocode:
balance = 200
for txn in transactions:
  balance = txn
ALoop does not iterate over transactions
BBalance is overwritten instead of updated
CInitial balance is set incorrectly
DTransactions list is empty
Step-by-Step Solution
Solution:
  1. Step 1: Review code logic

    The loop assigns balance to the current transaction value each iteration.
  2. Step 2: Identify issue

    This overwrites balance instead of accumulating transaction amounts.
  3. Step 3: Correct approach

    Use balance += txn to add each transaction to balance.
  4. Final Answer:

    Balance is overwritten instead of updated -> Option B
  5. Quick Check:

    Assignment '=' replaces value; use '+=' to accumulate [OK]
Quick Trick: Use '+=' to add, not '=' to overwrite balance [OK]
Common Mistakes:
  • Using '=' instead of '+=' inside loop
  • Ignoring accumulation logic
  • Assuming balance resets automatically

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More LLD Quizzes