Coding problem
A running balance for the statement
The task
A bank statement shows every transaction with the balance right after it.
Complete RunningBalance(decimal opening, List<Transaction> transactions). Transaction is a record with an Id and an Amount: a deposit is positive and a withdrawal negative. The transactions are in date order. Return a StatementLine for each transaction, in that order, with its Balance after the transaction: the first balance is opening plus the first amount, and each later balance is the previous balance plus that transaction's amount. A balance may go below zero. An empty list gives an empty list. The list and its transactions are never null.
Signature
public record Transaction(int Id, decimal Amount); public record StatementLine(int Id, decimal Amount, decimal Balance); public static List<StatementLine> RunningBalance(decimal opening, List<Transaction> transactions)
Examples
opening 100, transactions [(1, 50), (2, -30), (3, -200)]→[(1, 50, 150), (2, -30, 120), (3, -200, -80)]. Each balance is the one before plus the amount, and it can go below zero.opening 25, transactions []→[]. No transactions, no lines.
Limits: Up to 200,000 transactions.
The tests check: no transactions, the opening balance as the start, the balance after each transaction rather than before, a balance below zero, cents adding up exactly, the lines keeping their order, and 200,000 transactions in time.
Hints
Your code
Already included: using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net.Http; using System.Threading; using System.Threading.Tasks;
public record Transaction(int Id, decimal Amount);
public record StatementLine(int Id, decimal Amount, decimal Balance);
public static class Solution
{
// Each transaction with the balance after it, starting from the opening balance.
public static List<StatementLine> RunningBalance(decimal opening, List<Transaction> transactions)
{
throw new NotImplementedException();
}
}This problem comes with TechInterviewNotes Full Access
Unlock all paid content on TechInterviewNotes.