Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Apply Multiple Array Transactions

EasyPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Apply Multiple Array Transactions. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

In an Addepar portfolio model, each transaction transfers cash between two accounts. Given the starting balance of every account and exactly three ordered transactions, compute the final balance of each account after applying the transactions in order.

Each transaction is represented as [from_account, to_account, amount]. Deduct amount from the source account and add it to the destination account. Account balances must be updated immediately, because later transactions may depend on earlier transfers.

Formal Specification

Implement apply_transactions(balances, transactions), where balances is a list of nonnegative integers and transactions is a list containing exactly three transaction arrays. Return a new list containing the final balance for every account. Account IDs are zero-based indices into balances.

You may assume every transaction references valid accounts, the source and destination are different, and the source account has sufficient funds when its transaction is applied.

Constraints

  • 1 <= len(balances) <= 10^5
  • len(transactions) == 3
  • 0 <= balances[i] <= 10^12
  • 0 <= amount <= 10^12
  • Every account ID is valid
  • Every source account has sufficient funds when its transaction is applied

Function Signature

def apply_transactions(balances, transactions):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output