Bird
Raised Fist0

Identify the bug in this Expense class method that calculates each user's share:

medium📝 Analysis Q14 of Q15
LLD - Design — Splitwise (Expense Sharing)
Identify the bug in this Expense class method that calculates each user's share:
class Expense:
    def __init__(self, amount, paid_by, split_between):
        self.amount = amount
        self.paid_by = paid_by
        self.split_between = split_between

    def split_amount(self):
        return self.amount // len(self.split_between)
AUsing integer division (//) may lose cents in split
Bsplit_between should be a dictionary, not a list
Cpaid_by should be a list, not a single user
Damount should be a string, not a number
Step-by-Step Solution
Solution:
  1. Step 1: Analyze the division operator used

    The method uses integer division (//) which truncates decimals.
  2. Step 2: Understand impact on money split

    Using // can lose fractional cents, causing inaccurate splits.
  3. Final Answer:

    Using integer division (//) may lose cents in split -> Option A
  4. Quick Check:

    Integer division truncates decimals, causing loss [OK]
Quick Trick: Use float division (/) for accurate money splits [OK]
Common Mistakes:
MISTAKES
  • Ignoring decimal loss from integer division
  • Confusing data types for paid_by or split_between
  • Thinking amount should be string

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More LLD Quizzes