PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep

Quick Overview

This question evaluates a candidate's ability to implement stateful transaction processing, accurate integer bookkeeping across accounts and currencies, stable time-ordered sorting, and complex business rules such as overdraft rejection and platform-backed borrowing.

  • medium
  • Stripe
  • Coding & Algorithms
  • Software Engineer

Process Transactions and Compute Balances

Company: Stripe

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

You are given a list of financial transactions in CSV-like rows: ``` account_name, timestamp, currency, amount ``` - `account_name` is a string ID. - `timestamp` is an integer (not guaranteed to be sorted). - `currency` is a string like `"usd"`. - `amount` is an integer (positive = credit, negative = debit). Assume all arithmetic is integer. Unless otherwise stated, process transactions in increasing `timestamp` order (stable tie-breaker by input order). ## Part 1 — Final non-zero balances Compute each account’s final balance (you may assume all rows are the same currency, or treat balances independently per `(account_name, currency)`—state your assumption). Output all accounts with **non-zero** final balance. **Example** Input: ``` account_name, timestamp, currency, amount acct_123,1,usd,1000 acct_123,2,usd,500 acct_321,3,usd,400 acct_321,4,usd,-400 ``` Output: - `acct_123` has balance `1500` - `acct_321` is omitted (balance `0`) ## Part 2 — Reject debits that would overdraw Process transactions in time order while maintaining balances. A transaction is **rejected** if applying it would make the account’s balance negative (i.e., `balance + amount < 0`). Rejected transactions: - must be appended to `rejected_transactions` in the order encountered, - must **not** affect any balances. At the end, output: 1) `rejected_transactions` (full original rows for rejected ones), and 2) all accounts with **non-zero** final balance. **Example** Input: ``` account_name, timestamp, currency, amount acct_123,1,usd,1000 acct_123,2,usd,500 acct_321,3,usd,400 acct_321,4,usd,-500 ``` Output: - Non-zero balances: `acct_123 = 1500`, `acct_321 = 400` - `rejected_transactions = ["acct_321,4,usd,-500"]` ## Part 3(a) — Platform reserve can cover negatives You are also given a special `platform_id` account. Process transactions in time order with the following rule: - For any **non-platform** account, if applying a transaction would make its balance negative, the account may **borrow from the platform** just enough to bring its balance back to `0`. - This borrowing reduces the platform’s balance accordingly. - Track the platform’s **outstanding loans** (e.g., per account) so that when an account later receives credits, it repays its debt before increasing its own positive balance (state and apply a consistent repayment rule). - If the platform does not have enough available balance to cover the needed borrowing at that moment, then the transaction is **rejected** (and does not change balances or debts). Output three things: 1) `max_reserve`: the **maximum total outstanding amount borrowed from the platform at any time** (peak platform exposure), 2) `rejected_transactions`, 3) all accounts with **non-zero** final balance (including the platform if non-zero). **Example** (`platform_id = acct_123`) Input: ``` account_name, timestamp, currency, amount acct_123,1,usd,1000 acct_321,3,usd,400 acct_321,4,usd,-500 ``` Explanation: `acct_321` would go to `-100`, so it borrows `100` from the platform to return to `0`. Output: - `max_reserve = 100` - `rejected_transactions = []` - Non-zero balances include `acct_123 = 900` (and `acct_321` omitted if it ends at `0`) ### Constraints (you may assume) - Up to ~200k transactions. - Need an efficient solution (typically `O(n log n)` due to sorting, or `O(n)` if already sorted).

Quick Answer: This question evaluates a candidate's ability to implement stateful transaction processing, accurate integer bookkeeping across accounts and currencies, stable time-ordered sorting, and complex business rules such as overdraft rejection and platform-backed borrowing.

Part 1: Final Non-Zero Balances

Given a list of transaction rows, compute each account's final balance. Each row is a CSV-like string with four fields: account_name,timestamp,currency,amount. Transactions may be unsorted, but for this part the final sum is independent of order. Assume all rows are in the same currency, so balances are tracked by account_name only. Return only accounts whose final balance is non-zero.

Constraints

  • 0 <= len(transactions) <= 200000
  • account_name and currency contain no commas
  • timestamp is a valid integer
  • amount is a valid integer
  • All rows may be treated as the same currency for this problem

Examples

Input: (['acct_123,1,usd,1000', 'acct_123,2,usd,500', 'acct_321,3,usd,400', 'acct_321,4,usd,-400'],)

Expected Output: {'acct_123': 1500}

Explanation: acct_123 ends with 1500. acct_321 ends with 0 and is omitted.

Input: ([],)

Expected Output: {}

Explanation: No transactions means no non-zero balances.

Input: (['b,5,usd,10', 'a,1,usd,-3', 'b,2,usd,-10', 'a,4,usd,3'],)

Expected Output: {}

Explanation: Both accounts net to 0, even though the input is unsorted.

Input: (['acct_single,10,usd,-50'],)

Expected Output: {'acct_single': -50}

Explanation: Part 1 does not reject overdrafts; it only computes final sums.

Hints

  1. Use a hash map from account name to running balance.
  2. After summing all rows, filter out entries whose balance is exactly 0.

Part 2: Reject Debits That Would Overdraw

Given a list of transaction rows, process them in increasing timestamp order. If two transactions have the same timestamp, preserve their original input order. Each row is a CSV-like string with four fields: account_name,timestamp,currency,amount. Assume all rows are in the same currency, so balances are tracked by account_name only. A transaction is rejected if applying it would make that account's balance negative. Rejected transactions do not affect balances and must be returned as their original row strings in the order they are encountered during processing.

Constraints

  • 0 <= len(transactions) <= 200000
  • account_name and currency contain no commas
  • timestamp is a valid integer
  • amount is a valid integer
  • All accounts start with balance 0
  • All rows may be treated as the same currency for this problem

Examples

Input: (['acct_123,1,usd,1000', 'acct_123,2,usd,500', 'acct_321,3,usd,400', 'acct_321,4,usd,-500'],)

Expected Output: {'rejected_transactions': ['acct_321,4,usd,-500'], 'balances': {'acct_123': 1500, 'acct_321': 400}}

Explanation: The final debit for acct_321 would make its balance -100, so it is rejected.

Input: ([],)

Expected Output: {'rejected_transactions': [], 'balances': {}}

Explanation: No transactions means no rejections and no balances.

Input: (['acct,2,usd,-50', 'acct,1,usd,40', 'acct,2,usd,20', 'acct,2,usd,-70'],)

Expected Output: {'rejected_transactions': ['acct,2,usd,-50', 'acct,2,usd,-70'], 'balances': {'acct': 60}}

Explanation: After sorting, the timestamp-1 credit is processed first. Among timestamp-2 rows, original order is preserved.

Input: (['a,1,usd,100', 'a,2,usd,-100', 'b,3,usd,-1'],)

Expected Output: {'rejected_transactions': ['b,3,usd,-1'], 'balances': {}}

Explanation: A transaction that brings a balance exactly to 0 is accepted. The debit for b is rejected.

Input: (['only,7,usd,-10'],)

Expected Output: {'rejected_transactions': ['only,7,usd,-10'], 'balances': {}}

Explanation: A single debit from a zero balance would overdraw, so it is rejected.

Hints

  1. Sort by the pair (timestamp, original_index) before processing.
  2. Only update an account's balance if the transaction is accepted.

Part 3: Platform Reserve Can Cover Negatives

Given a list of transaction rows and a special platform account ID, process transactions in increasing timestamp order. If two transactions have the same timestamp, preserve their original input order. Assume all rows are in the same currency, so balances are tracked by account_name only. Non-platform accounts may not end a transaction with a negative balance. If a non-platform transaction would make the account negative, the account may borrow exactly enough from the platform to bring its balance back to 0. Borrowing decreases the platform's available balance and increases that account's outstanding debt. Later credits to that account first repay its debt to the platform; only leftover credit increases the account's own balance. If the platform does not have enough available balance to cover required borrowing, reject the transaction. Platform transactions affect the platform's available balance directly; a platform transaction that would make the platform balance negative is rejected.

Constraints

  • 0 <= len(transactions) <= 200000
  • account_name and currency contain no commas
  • timestamp is a valid integer
  • amount is a valid integer
  • All accounts, including the platform, start with balance 0
  • All rows may be treated as the same currency for this problem

Examples

Input: (['acct_123,1,usd,1000', 'acct_321,3,usd,400', 'acct_321,4,usd,-500'], 'acct_123')

Expected Output: {'max_reserve': 100, 'rejected_transactions': [], 'balances': {'acct_123': 900}}

Explanation: acct_321 needs to borrow 100 from the platform, so platform balance falls from 1000 to 900.

Input: (['platform,1,usd,500', 'acct,2,usd,-200', 'acct,3,usd,50', 'acct,4,usd,300'], 'platform')

Expected Output: {'max_reserve': 200, 'rejected_transactions': [], 'balances': {'platform': 500, 'acct': 150}}

Explanation: acct borrows 200, repays 50, then repays the remaining 150 and keeps 150.

Input: (['p,1,usd,100', 'a,2,usd,-150', 'a,3,usd,-80', 'a,4,usd,50'], 'p')

Expected Output: {'max_reserve': 80, 'rejected_transactions': ['a,2,usd,-150'], 'balances': {'p': 70}}

Explanation: The first debit needs 150 but platform has only 100, so it is rejected. The later debit borrows 80; then 50 is repaid.

Input: ([], 'platform')

Expected Output: {'max_reserve': 0, 'rejected_transactions': [], 'balances': {}}

Explanation: No transactions means no loans, rejections, or balances.

Input: (['p,1,usd,50', 'p,2,usd,-60', 'a,3,usd,-40'], 'p')

Expected Output: {'max_reserve': 40, 'rejected_transactions': ['p,2,usd,-60'], 'balances': {'p': 10}}

Explanation: The platform's own overdrawing debit is rejected. It can still lend 40 afterward.

Hints

  1. Maintain three pieces of state: available balances, per-account debts, and total outstanding debt.
  2. For a positive non-platform transaction, repay existing debt before adding anything to the account's balance.
Last updated: Jun 22, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,000+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Assign Reviewers from Changed Files - Stripe (medium)
  • Generate Account Email Notifications - Stripe (medium)
  • Calculate Transaction Fees - Stripe (medium)
  • Build an Account Transfer Ledger - Stripe (medium)
  • Implement Validation and String Compression - Stripe (hard)