Bird
0
0

Identify the error in this pseudocode snippet for simplifying debts:

medium📝 Analysis Q6 of 15
LLD - Design — Splitwise (Expense Sharing)
Identify the error in this pseudocode snippet for simplifying debts:
for person in net_balances:
  if net_balances[person] > 0:
    for debtor in net_balances:
      if net_balances[debtor] < 0:
        net_balances[person] -= net_balances[debtor]
        net_balances[debtor] = 0
AIt misses updating the debtor's balance after payment
BIt incorrectly subtracts negative values, increasing the creditor's balance
CIt loops over the same dictionary twice unnecessarily
DIt does not reset creditor's balance to zero after transactions
Step-by-Step Solution
Solution:
  1. Step 1: Analyze subtraction of negative values

    Subtracting a negative number increases the creditor's balance incorrectly.
  2. Step 2: Identify correct operation

    The code should add the debtor's absolute value or subtract the positive amount owed.
  3. Final Answer:

    It incorrectly subtracts negative values, increasing the creditor's balance -> Option B
  4. Quick Check:

    Subtracting negative = Incorrect balance update [OK]
Quick Trick: Subtracting negatives adds value; use absolute values instead [OK]
Common Mistakes:
  • Ignoring sign when updating balances
  • Not resetting balances properly
  • Confusing creditor and debtor roles

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More LLD Quizzes