PracHub
QuestionsPremiumLearningGuidesCheatsheetNEWCoaches

Quick Overview

This question evaluates transactional state management, time-ordered stream processing, balance reconciliation, handling of rejected transactions, and modeling inter-account borrowing with platform reserve accounting.

  • medium
  • TikTok
  • Coding & Algorithms
  • Software Engineer

Compute balances, rejections, and platform reserve

Company: TikTok

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

You are given a set of transactions in CSV-like format with columns: - `account_name`, `timestamp`, `currency`, `amount` Assume: - Transactions for all accounts can be interleaved; you must process them in **ascending timestamp order** (if input is already ordered, you may process in given order). - All amounts are integers in the smallest currency unit (e.g., cents). - Treat each `(account_name, currency)` as a separate balance unless stated otherwise. ## Part 1 — Non-zero ending balances Compute each account’s ending balance (sum of its amounts). Output all accounts whose ending balance is non-zero, along with the balance. ## Part 2 — Reject transactions that would make balance negative Process transactions in time order while maintaining balances. - If applying a transaction would make the account’s balance negative, **reject** it (do not apply it). - Collect rejected transactions as strings in the original row format (or a clear equivalent such as `"acct,timestamp,currency,amount"`). Output: 1. The rejected transaction list. 2. All accounts with non-zero ending balances after applying only accepted transactions. ## Part 3(a) — Allow borrowing from a platform account You are also given a `platform_id`. Process transactions in time order with this additional rule: - For any **non-platform** account, if after applying a transaction its balance would become negative, the account may “borrow” from the platform to bring it back up to 0 **as long as the platform has sufficient funds**. - Borrow amount = `-(new_balance)`. - Decrease the platform’s balance by the borrowed amount. - The transaction is accepted if borrowing succeeds. - If the platform does not have enough balance to cover the borrow, the transaction is rejected (and not applied). Additionally compute: - `max_reserve`: the **maximum total amount borrowed from the platform at any point in time** (i.e., peak borrowed amount outstanding). You should define and track this consistently based on your borrowing model. Output for Part 3(a): 1. `max_reserve` 2. `rejected_transactions` 3. All accounts with non-zero ending balances after processing. ## Notes - Clearly document how you compute `max_reserve` (e.g., peak cumulative borrowed amount across accounts over time). - If multiple currencies exist, state whether borrowing is allowed only within the same currency (recommended) or across currencies (not recommended). Implement the chosen assumption consistently.

Quick Answer: This question evaluates transactional state management, time-ordered stream processing, balance reconciliation, handling of rejected transactions, and modeling inter-account borrowing with platform reserve accounting.

Part 1: Non-zero ending balances

You are given a list of transactions, where each transaction is a 4-tuple (account_name, timestamp, currency, amount). Treat each (account_name, currency) pair as a separate balance. Compute the ending balance for every pair by summing all amounts, and return only the pairs whose final balance is non-zero. For deterministic output, sort the result by account_name, then currency.

Constraints

  • 0 <= len(transactions) <= 200000
  • account_name and currency are non-empty strings
  • timestamp is an integer
  • amount is an integer in the smallest currency unit and may be negative, zero, or positive
  • Different currencies for the same account must be tracked separately

Examples

Input: ([('alice', 3, 'USD', 100), ('bob', 1, 'USD', 50), ('alice', 2, 'USD', -20), ('alice', 4, 'EUR', 70), ('bob', 5, 'USD', -50), ('carol', 6, 'USD', 0)],)

Expected Output: [('alice', 'EUR', 70), ('alice', 'USD', 80)]

Explanation: bob and carol end at 0, so they are omitted.

Input: ([('acct', 1, 'USD', -30)],)

Expected Output: [('acct', 'USD', -30)]

Explanation: A negative ending balance is still included if it is non-zero.

Input: ([('x', 1, 'USD', 40), ('x', 2, 'USD', -40), ('x', 3, 'EUR', 10), ('x', 4, 'EUR', -10)],)

Expected Output: []

Explanation: All balances cancel to zero.

Input: ([],)

Expected Output: []

Explanation: Edge case: no transactions.

Hints

  1. A dictionary keyed by (account_name, currency) is enough to accumulate balances.
  2. After processing all rows, filter out balances equal to 0 before returning.

Part 2: Reject transactions that would make balance negative

You are given a list of transactions, where each transaction is a 4-tuple (account_name, timestamp, currency, amount). Process all rows in ascending timestamp order. If two rows have the same timestamp, preserve their original input order. Each (account_name, currency) pair has its own balance, starting at 0. If applying a transaction would make that balance negative, reject the transaction and do not apply it. Return the rejected rows and the final non-zero balances after processing only accepted transactions.

Constraints

  • 0 <= len(transactions) <= 200000
  • account_name and currency are non-empty strings
  • timestamp is an integer; process in ascending timestamp order
  • If timestamps tie, preserve the original input order
  • amount is an integer in the smallest currency unit

Examples

Input: ([('alice', 3, 'USD', -70), ('alice', 1, 'USD', 100), ('alice', 2, 'USD', -30), ('bob', 4, 'EUR', -10), ('bob', 5, 'EUR', 20), ('bob', 6, 'EUR', -5)],)

Expected Output: (['bob,4,EUR,-10'], [('bob', 'EUR', 15)])

Explanation: alice finishes at 0 and is omitted; bob's first transaction is rejected because it would make balance negative.

Input: ([('a', 1, 'USD', 50), ('a', 1, 'USD', -70), ('a', 1, 'USD', 20)],)

Expected Output: (['a,1,USD,-70'], [('a', 'USD', 70)])

Explanation: All timestamps tie, so input order matters: 50 is applied, -70 is rejected, then 20 is applied.

Input: ([('x', 1, 'USD', 100), ('x', 2, 'EUR', -10), ('x', 3, 'USD', -40), ('x', 4, 'EUR', 15)],)

Expected Output: (['x,2,EUR,-10'], [('x', 'EUR', 15), ('x', 'USD', 60)])

Explanation: USD and EUR balances are tracked separately.

Input: ([],)

Expected Output: ([], [])

Explanation: Edge case: no transactions.

Hints

  1. Carry each row's original index while sorting so equal timestamps stay in input order.
  2. A rejected transaction should not change the stored balance at all.

Part 3: Allow borrowing from a platform account and compute max_reserve

You are given a list of transactions and a platform_id. Process all rows in ascending timestamp order, preserving original input order when timestamps tie. Each (account_name, currency) pair has its own cash balance, starting at 0. Borrowing is allowed only within the same currency. For a non-platform account, if a transaction would make its cash balance negative, it may borrow exactly enough from the platform account in that currency to bring its cash balance back to 0, but only if the platform has enough cash in that currency. If borrowing succeeds, the transaction is accepted. If the platform does not have enough cash, reject the transaction. The platform account itself cannot borrow; if one of its own transactions would make its balance negative, reject it. Also track outstanding debt per non-platform account and currency. Any positive transaction into a non-platform account first repays that account's debt to the platform in the same currency, and only the remainder increases the account's cash balance. Define max_reserve[currency] as the peak total outstanding borrowed amount in that currency across all non-platform accounts at any moment. Return max_reserve, the rejected rows, and the final non-zero cash balances.

Constraints

  • 0 <= len(transactions) <= 200000
  • account_name, currency, and platform_id are non-empty strings
  • timestamp is an integer; process in ascending timestamp order
  • If timestamps tie, preserve the original input order
  • amount is an integer in the smallest currency unit
  • Borrowing is allowed only within the same currency
  • The platform account itself cannot borrow

Examples

Input: ([('platform', 1, 'USD', 100), ('alice', 2, 'USD', -30), ('alice', 3, 'USD', 10), ('alice', 4, 'USD', -50), ('alice', 5, 'USD', 100)], 'platform')

Expected Output: ({'USD': 70}, [], [('alice', 'USD', 30), ('platform', 'USD', 100)])

Explanation: alice borrows 30, repays 10, later borrows 50 more, so the peak outstanding reserve in USD is 70.

Input: ([('platform', 1, 'USD', 40), ('bob', 2, 'USD', -50), ('platform', 3, 'USD', -10), ('platform', 4, 'USD', -35), ('bob', 5, 'USD', -20)], 'platform')

Expected Output: ({'USD': 20}, ['bob,2,USD,-50', 'platform,4,USD,-35'], [('platform', 'USD', 10)])

Explanation: bob's first withdrawal is rejected because the platform cannot fund it; the platform's own overdrawn transaction is also rejected.

Input: ([('platform', 1, 'USD', 100), ('platform', 1, 'EUR', 50), ('alice', 2, 'USD', -70), ('bob', 3, 'EUR', -20), ('alice', 4, 'USD', 30), ('bob', 5, 'EUR', 50)], 'platform')

Expected Output: ({'EUR': 20, 'USD': 70}, [], [('bob', 'EUR', 30), ('platform', 'EUR', 50), ('platform', 'USD', 60)])

Explanation: Reserve is tracked separately per currency. alice's USD debt is partially repaid; bob fully repays EUR debt and keeps the remainder.

Input: ([], 'platform')

Expected Output: ({}, [], [])

Explanation: Edge case: no transactions.

Hints

  1. Track two separate things for non-platform accounts: cash balance and outstanding debt.
  2. When money flows into a borrower account, repay debt first; max_reserve should be based on total outstanding debt, not on platform cash.
Last updated: May 12, 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

  • Parse a nested list from a string - TikTok (medium)
  • Implement stacks, streaming median, and upward path sum - TikTok (easy)
  • Maximize sum with no adjacent elements - TikTok (medium)
  • Implement stack variants and path-sum check - TikTok (medium)
  • Find the longest palindromic substring - TikTok (easy)