Bird
0
0

Find the bug in this Expense class method:

medium📝 Analysis Q7 of 15
LLD - Design — Splitwise (Expense Sharing)
Find the bug in this Expense class method:
class Expense:
    def __init__(self, amount, paid_by):
        self.amount = amount
        self.paid_by = paid_by
    def split(self, users):
        share = self.amount / len(users)
        for user in users:
            user.balance -= share
ADivision by zero error possible
Bbalance attribute may not exist in User
CMethod split should return share
DUsing -= instead of +=
Step-by-Step Solution
Solution:
  1. Step 1: Analyze split method logic

    It subtracts share from user.balance, assuming balance attribute exists.
  2. Step 2: Identify potential attribute error

    If User class lacks balance attribute, this causes runtime error.
  3. Final Answer:

    balance attribute may not exist in User -> Option B
  4. Quick Check:

    Check User class for balance attribute [OK]
Quick Trick: Ensure User has balance attribute before modifying it [OK]
Common Mistakes:
  • Ignoring attribute existence
  • Assuming split must return value
  • Confusing += and -= operators

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More LLD Quizzes