Compute positive net balances per user from transaction records

Quick Overview

Parse comma-separated transaction records, compute each user's net balance from credits and debits, and return the users whose balance is positive in a fixed order. It tests hash map aggregation, filtering on a strict condition, and sorting on a numeric key with a string tie-break.

Compute positive net balances per user from transaction records

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

You are given a list of transaction records `transactions`. Each record is a string in the format `"userId,amount,type"`, where `userId` is a non-empty alphanumeric string, `amount` is a positive integer, and `type` is either `"credit"` or `"debit"`. For each user, compute the net balance: the sum of that user's `credit` amounts minus the sum of their `debit` amounts. Include only users whose net balance is strictly positive. Return the result as a list of `[userId, netBalance]` pairs, with `netBalance` written as a decimal string. Sort the pairs by net balance in descending order and break ties by `userId` in ascending lexicographic order. ### Function Signature ```python def parse_transactions(transactions: list[str]) -> list[list[str]]: ``` ### Rules - Each record has exactly three comma-separated fields with no surrounding spaces: user id, amount, type. - User ids are case-sensitive, so `"Alice"` and `"alice"` are different users. - Order by the numeric value of the net balance, so a balance of 10 comes before a balance of 9. - Compare user ids with ordinary string comparison by character code (digits before uppercase letters, uppercase before lowercase). - Each output pair is `[userId, str(netBalance)]`, for example `["alice", "70"]`. - If no user has a positive net balance, return an empty list. ### Constraints - `1 <= transactions.length <= 10⁴` - Each transaction is formatted as `"userId,amount,type"`. - `1 <= amount <= 10⁶` - `type` is either `"credit"` or `"debit"`. - `userId` is a non-empty alphanumeric string. ### Examples ```text Input: transactions = ["alice,100,credit","bob,50,credit","alice,30,debit","bob,80,debit"] Output: [["alice","70"]] ``` Alice's net balance is 100 - 30 = 70. Bob's is 50 - 80 = -30, which is not positive, so Bob is left out. ```text Input: transactions = ["u1,200,credit","u2,200,credit","u1,50,debit","u2,50,debit","u3,500,credit","u3,500,debit"] Output: [["u1","150"],["u2","150"]] ``` `u1` and `u2` both end at 150, and `u3` ends at exactly 0, so it is excluded. The tie at 150 is broken by user id, which puts `u1` first. ```text Input: transactions = ["dave,9,credit","carol,40,credit","erin,10,debit","carol,30,debit"] Output: [["carol","10"],["dave","9"]] ``` Carol ends at 40 - 30 = 10, Dave at 9 and Erin at -10. Carol comes first because 10 is the larger number, even though a descending sort on the strings would put `"9"` ahead of `"10"`.

Overview: Parse comma-separated transaction records, compute each user's net balance from credits and debits, and return the users whose balance is positive in a fixed order. It tests hash map aggregation, filtering on a strict condition, and sorting on a numeric key with a string tie-break.

PracHub logo
General
Nov 25, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

You are given a list of transaction records transactions. Each record is a string in the format "userId,amount,type", where userId is a non-empty alphanumeric string, amount is a positive integer, and type is either "credit" or "debit".

For each user, compute the net balance: the sum of that user's credit amounts minus the sum of their debit amounts. Include only users whose net balance is strictly positive.

Return the result as a list of [userId, netBalance] pairs, with netBalance written as a decimal string. Sort the pairs by net balance in descending order and break ties by userId in ascending lexicographic order.

Function Signature

def parse_transactions(transactions: list[str]) -> list[list[str]]:

Rules

  • Each record has exactly three comma-separated fields with no surrounding spaces: user id, amount, type.
  • User ids are case-sensitive, so "Alice" and "alice" are different users.
  • Order by the numeric value of the net balance, so a balance of 10 comes before a balance of 9.
  • Compare user ids with ordinary string comparison by character code (digits before uppercase letters, uppercase before lowercase).
  • Each output pair is [userId, str(netBalance)] , for example ["alice", "70"] .
  • If no user has a positive net balance, return an empty list.

Constraints

  • 1 <= transactions.length <= 10⁴
  • Each transaction is formatted as "userId,amount,type" .
  • 1 <= amount <= 10⁶
  • type is either "credit" or "debit" .
  • userId is a non-empty alphanumeric string.

Examples

Input: transactions = ["alice,100,credit","bob,50,credit","alice,30,debit","bob,80,debit"]
Output: [["alice","70"]]

Alice's net balance is 100 - 30 = 70. Bob's is 50 - 80 = -30, which is not positive, so Bob is left out.

Input: transactions = ["u1,200,credit","u2,200,credit","u1,50,debit","u2,50,debit","u3,500,credit","u3,500,debit"]
Output: [["u1","150"],["u2","150"]]

u1 and u2 both end at 150, and u3 ends at exactly 0, so it is excluded. The tie at 150 is broken by user id, which puts u1 first.

Input: transactions = ["dave,9,credit","carol,40,credit","erin,10,debit","carol,30,debit"]
Output: [["carol","10"],["dave","9"]]

Carol ends at 40 - 30 = 10, Dave at 9 and Erin at -10. Carol comes first because 10 is the larger number, even though a descending sort on the strings would put "9" ahead of "10".