Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Trending in AI
Hot
Agentic AI EngineerForward-Deployed EngineerGenAI EngineerAI Architect
Browse
Browse All RolesEvery role hub, from analyst to MLBrowse All CompaniesCompany-specific interview loopsAll Interview GuidesThe full guide library
Top questions by role
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Top questions by skill
SQLPythonStatisticsMachine LearningA/B TestingSystem DesignGenerative AIProduct SenseMetricsBehavioral
Browse all questions →Try a mock interview
ExperiencesCoaching
New
Practice
Mock InterviewsTimed interview simulations with feedbackSuccess PathYour 6-week structured planModulesCurated lessons by topicWebinarsTalks from ex-Big Tech data leadsPlaygroundA free-form scratch editor
Learn
BlogInterview strategy and career adviceTech Job Market ReportHiring trends across data and AI rolesFor UniversitiesDataford for career centersAbout DatafordWho we are and how we build
Pricing
Start free
Splitwise Machine Coding
00:00
5 left

Splitwise Machine Coding

HardPython

Problem

Build the settlement engine for a Splitwise-like group used by Swiggy employees. Given participants and recorded expenses, calculate each person's net balance and return a set of money transfers that settles the group using the minimum possible number of transactions.

A positive balance means the participant should receive money. A negative balance means the participant owes money. Every expense amount and share is an integer number of paise.

Formal Specification

Implement settle_debts(participants, expenses).

  • participants is a list of unique participant names.
  • expenses is a list of dictionaries with keys payer, amount, and shares.
  • shares maps every participant who owes part of that expense to an integer share. Shares for an expense sum to its amount.
  • Return a list of dictionaries with keys from, to, and amount.
  • Every returned transfer must have a positive amount.
  • The result must settle all balances and use the minimum number of transfers.

Because finding the globally minimum number of settlements is combinatorial, use exact backtracking for the input limits below.

Constraints

  • 1 <= len(participants) <= 10
  • 0 <= len(expenses) <= 50
  • Expense amounts and shares are non-negative integers measured in paise
  • Each expense's shares sum to its amount
  • Every participant appears in every expense's shares map

Function Signature

def settle_debts(participants, expenses):
Interviewer

Whenever you're ready, walk me through your approach or go straight to the code. The requirements are in the Question tab.

Sign up free to run or submit. Log in
Code
Sign up free to run your code. Your draft is saved.Run with a free accountLog inLn 2
Run your code to see test output here.