Getting everything ready for you…

Loading courses, problems and your progress. This takes just a moment.

0s

Projects › Intermediate

Group Expense Splitter

Track shared expenses in a group trip or flat and settle up with the fewest possible payments.

Level: Intermediate · Time: 1–2 weeks · Tech stack: React, Node.js, MongoDB, Greedy algorithm

Features to build

What you will learn

Step-by-step guide

1. Store money as integers

Keep amounts in paise (₹12.50 → 1250) to avoid floating-point errors like 0.1 + 0.2.

2. Expense model

{ group, paidBy, amount, splits: [{ user, share }], note, date } — shares must add up to the amount (handle the leftover paise by giving it to the first person).

3. Net balance

net[user] = paid - owed. Positive = should receive, negative = should pay.

4. Settle up with fewest payments (greedy)

function settle(net) {
  const give = [], take = [], txns = [];
  for (const [u, v] of Object.entries(net)) (v < 0 ? give : take).push([u, Math.abs(v)]);
  give.sort((a, b) => b[1] - a[1]); take.sort((a, b) => b[1] - a[1]);
  while (give.length && take.length) {
    const [g, gv] = give[0], [t, tv] = take[0], amt = Math.min(gv, tv);
    txns.push({ from: g, to: t, amount: amt });
    give[0][1] -= amt; take[0][1] -= amt;
    if (!give[0][1]) give.shift(); if (!take[0][1]) take.shift();
  }
  return txns;
}

(Matching the largest debtor with the largest creditor keeps the list short; re-sort or use heaps after each step.)

5. Permissions

Only group members can see or add expenses — check membership on every API call.

6. UI

Group page with balances in green/red, an "Add expense" modal and a settle-up list with "Mark as paid".

Level it up

On your resume

Learn on Codify