PracHub
QuestionsPremiumLearningGuidesInterview PrepNEWCoaches

Quick Overview

This question evaluates transaction processing, ledger state management, handling of edge cases like insufficient funds and fixed fees, and efficient use of data structures and integer arithmetic.

  • medium
  • Stripe
  • Coding & Algorithms
  • Software Engineer

Compute balances with transaction fees and rejections

Company: Stripe

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Onsite

You are building a simple ledger. ## Inputs - A list of account names. - An initial balance for each account. - A list of transfer transactions, each with: - `from` (sender) - `to` (receiver) - `amount` (positive integer) - A transaction fee rule: each successful transfer charges the sender a **fixed fee** `fee` (positive integer) in addition to the transferred `amount`. ## Processing rules For each transaction in order: 1. A transaction is **accepted** only if the sender has at least `amount + fee` available at that moment. 2. If accepted: - Subtract `amount + fee` from `from`. - Add `amount` to `to`. 3. If rejected: - Do not change any balances. - Record the rejected transaction in a rejection list. ## Output 1. Print (or return) the final balances for **only** the accounts whose final balance is **not 0**. 2. Return the **rejection list** (all rejected transactions, in original order). ## Constraints - Up to 10^5 transactions. - Account count up to 10^5. - Amounts and balances fit in 64-bit signed integers. ## Notes - If an account name appears in a transaction but was not in the initial list, treat its initial balance as 0. - You may output balances in any deterministic order (e.g., sorted by account name) as long as it is specified and consistent.

Quick Answer: This question evaluates transaction processing, ledger state management, handling of edge cases like insufficient funds and fixed fees, and efficient use of data structures and integer arithmetic.

You are given a list of account names, their initial balances, a list of transfer transactions, and a fixed transaction fee. Process the transactions in the given order. A transaction `(from_account, to_account, amount)` is accepted only if the sender currently has at least `amount + fee`. If accepted, subtract `amount + fee` from the sender and add `amount` to the receiver. If rejected, do not change any balances and record that transaction in a rejection list. Accounts that appear only in transactions start with balance `0`. Return the final balances for only the accounts whose final balance is not `0`, sorted lexicographically by account name, along with the rejection list in original order.

Constraints

  • 0 <= len(accounts) <= 10^5
  • 0 <= len(transactions) <= 10^5
  • len(accounts) == len(initial_balances)
  • Account names in `accounts` are unique
  • `amount > 0` and `fee > 0`
  • Balances, amounts, and fees fit in 64-bit signed integers

Examples

Input: (["alice", "bob"], [100, 20], [("alice", "bob", 30), ("bob", "charlie", 40), ("alice", "bob", 70)], 5)

Expected Output: ([('alice', 65), ('bob', 5), ('charlie', 40)], [('alice', 'bob', 70)])

Explanation: The first two transfers succeed. The last one is rejected because alice has 65, but needs 75. Final non-zero balances are sorted by account name.

Input: (["a"], [10], [("ghost", "a", 1), ("a", "b", 8), ("b", "a", 6)], 2)

Expected Output: ([('a', 6)], [('ghost', 'a', 1)])

Explanation: ghost is not in the initial list, so its balance is 0 and the first transfer is rejected. The next two transfers succeed, and account b ends at 0 so it is omitted from the final balances.

Input: ([], [], [], 3)

Expected Output: ([], [])

Explanation: With no accounts and no transactions, there are no balances to report and no rejections.

Input: (["x", "y"], [7, 0], [("x", "y", 5)], 2)

Expected Output: ([('y', 5)], [])

Explanation: x has exactly amount + fee = 7, so the transfer is accepted. x ends at 0 and is omitted.

Input: (["self"], [10], [("self", "self", 7)], 3)

Expected Output: ([('self', 7)], [])

Explanation: A transfer to the same account is allowed. The net effect is only the fee being lost, so self goes from 10 to 7.

Hints

  1. Use a hash map from account name to current balance so each transaction can be checked and updated in O(1) average time.
  2. Process transactions strictly in order, and only sort the remaining non-zero balances once at the end.
Last updated: May 16, 2026

Loading coding console...

PracHub

Master your tech interviews with 7,500+ 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)