What if you could stop the headache of messy shared bills with just a few smart classes?
Why User, Group, Expense classes in LLD? - Purpose & Use Cases
Start learning this pattern below
Jump into concepts and practice - no test required
Imagine you are trying to track who owes what in a group of friends manually using just notes or spreadsheets.
You write down each person's name, the groups they belong to, and every expense they make. It quickly becomes a mess.
Manually managing users, groups, and expenses is slow and confusing.
It's easy to forget who paid for what or how to split costs fairly.
Errors happen often, and updating information takes too much time.
Using User, Group, and Expense classes organizes this data clearly.
Each user is an object, groups hold users, and expenses link to both.
This structure makes tracking, updating, and calculating shares simple and reliable.
users = ['Alice', 'Bob'] groups = {'Trip': ['Alice', 'Bob']} expenses = [('Alice', 'Trip', 100)]
class User: pass class Group: pass class Expense: pass
It enables building clear, scalable systems to manage shared expenses without confusion or errors.
Apps like Splitwise use these classes to help friends split bills easily and fairly.
Manual tracking is error-prone and slow.
Classes organize users, groups, and expenses logically.
This design makes managing shared costs easy and scalable.
Practice
Solution
Step 1: Understand the role of each class
User class represents individual people, Group holds multiple users, Expense tracks costs.Step 2: Identify which class stores individual info
User class stores personal details like name and ID for each person.Final Answer:
User -> Option BQuick Check:
User = Individual person [OK]
- Confusing Group with User
- Thinking Expense stores user details
- Assuming Payment is a class here
Solution
Step 1: Identify correct attribute for multiple users
A list is suitable to hold multiple User objects, so self.users = [] is correct.Step 2: Check other options for correctness
class Group: def __init__(self): self.user = {} uses a dict named user which is not typical for holding users; class Group: def __init__(self): self.expenses = [] uses expenses which belongs to Expense class; class Group: def __init__(self): self.members = None sets members to None which is not a collection.Final Answer:
class Group: def __init__(self): self.users = [] -> Option AQuick Check:
Group holds list of users = self.users = [] [OK]
- Using dict instead of list for users
- Confusing expenses with users
- Initializing members as None instead of a list
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)
expense = Expense(120, 'Alice', ['Alice', 'Bob', 'Charlie'])
print(expense.split_amount())Solution
Step 1: Understand the split_amount method
It divides total amount by number of people in split_between list.Step 2: Calculate the split
Amount = 120, split_between has 3 people, so 120 / 3 = 40.0.Final Answer:
40.0 -> Option DQuick Check:
120 divided by 3 = 40.0 [OK]
- Forgetting to divide by number of people
- Dividing by 2 instead of 3
- Assuming paid_by affects split amount
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)Solution
Step 1: Analyze the division operator used
The method uses integer division (//) which truncates decimals.Step 2: Understand impact on money split
Using // can lose fractional cents, causing inaccurate splits.Final Answer:
Using integer division (//) may lose cents in split -> Option AQuick Check:
Integer division truncates decimals, causing loss [OK]
- Ignoring decimal loss from integer division
- Confusing data types for paid_by or split_between
- Thinking amount should be string
Solution
Step 1: Identify responsibilities for each class
User holds personal info and balances, Group manages users and expenses, Expense tracks costs and splits.Step 2: Evaluate design for scalability and clarity
Create User, Group, and Expense classes where Group manages users and expenses; Expense tracks amount and split; User tracks individual balances updated by Group cleanly separates concerns, making it easier to maintain and scale.Final Answer:
Create User, Group, and Expense classes where Group manages users and expenses; Expense tracks amount and split; User tracks individual balances updated by Group -> Option CQuick Check:
Clear class roles = scalable design [OK]
- Putting all logic in one class
- Ignoring separation of concerns
- Using flat files for complex data
