Parse a transaction CSV and total each user's fees with country tax

Quick Overview

Parse a CSV string of payment transactions and compute each user's total fee from their completed rows, with a country-specific tax applied on top of the base fee. It tests careful string parsing, filtering, hash map aggregation and rounding money totals at the right step.

Parse a transaction CSV and total each user's fees with country tax

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

You are given a raw CSV string of transaction records. The first line is a header row, and every following line is one transaction with the fields `user_id`, `transaction_id`, `amount`, `fee_percentage`, `status` and `country`. Calculate the total fee for each user as the sum of `amount * fee_percentage / 100` over all of that user's completed transactions (status equals `"completed"`). Transactions with status `"pending"` or `"failed"` are ignored. A country-specific tax rate is applied on top of the base fee: - `"US"`: 10% tax - `"UK"`: 15% tax - `"IN"`: 5% tax - Any other country: 0% tax The final fee for a single transaction is `amount * fee_percentage / 100 * (1 + tax_rate)`. Return a dictionary mapping each `user_id` to their total fee, rounded to 2 decimal places. Only include users who have at least one completed transaction. ### Function Signature ```python def calculate_fees(csv_string: str) -> dict[str, float]: ``` ### Rules - Lines are separated by `\n`, and the columns appear in the order listed above. - The tax rate comes from each transaction's own `country`, so one user's transactions can be taxed at different rates. - Add up a user's unrounded per-transaction fees first, then round that total once to 2 decimal places, as Python's `round(total, 2)` does. - Key order in the returned dictionary does not matter. ### Constraints - The CSV string is non-empty and always contains a valid header row. - There is at least one data row. - All `amount` and `fee_percentage` values are non-negative numbers. - `status` is one of `"completed"`, `"pending"`, `"failed"`. - Country codes are uppercase two-letter strings. - `user_id` values are non-empty strings. - The CSV uses commas as delimiters, and no field is quoted or contains a comma. - `1 <= number of rows <= 10^4` ### Examples ```text Input: csv_string = "user_id,transaction_id,amount,fee_percentage,status,country\nalice,t1,1000,2.5,completed,US\nalice,t2,500,3.0,pending,US\nbob,t3,2000,1.5,completed,UK\nbob,t4,1500,2.0,completed,IN" Output: {"alice": 27.5, "bob": 66.0} ``` Alice has one completed transaction, `1000 * 2.5 / 100 * 1.10 = 27.5`, and her pending one is ignored. Bob's two completed transactions give `2000 * 1.5 / 100 * 1.15 = 34.5` and `1500 * 2.0 / 100 * 1.05 = 31.5`, for a total of 66.0. ```text Input: csv_string = "user_id,transaction_id,amount,fee_percentage,status,country\ncharlie,t5,800,5.0,failed,DE\ncharlie,t6,1200,3.0,completed,DE\ndave,t7,500,2.0,completed,US" Output: {"charlie": 36.0, "dave": 11.0} ``` Charlie's failed transaction is ignored, and his completed one is in DE, which has no tax: `1200 * 3.0 / 100 * 1.0 = 36.0`. Dave pays `500 * 2.0 / 100 * 1.10 = 11.0`. ```text Input: csv_string = "user_id,transaction_id,amount,fee_percentage,status,country\nerin,t8,333,0.1,completed,DE\nerin,t9,333,0.1,completed,FR\nerin,t10,333,0.1,completed,BR\nfrank,t11,400,2.0,pending,UK" Output: {"erin": 1.0} ``` Each of Erin's transactions costs `333 * 0.1 / 100 = 0.333` with no tax, so her total is 0.999, which rounds to 1.0. Rounding each transaction first would give 0.99. Frank has no completed transaction, so he is left out.

Overview: Parse a CSV string of payment transactions and compute each user's total fee from their completed rows, with a country-specific tax applied on top of the base fee. It tests careful string parsing, filtering, hash map aggregation and rounding money totals at the right step.

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

You are given a raw CSV string of transaction records. The first line is a header row, and every following line is one transaction with the fields user_id, transaction_id, amount, fee_percentage, status and country.

Calculate the total fee for each user as the sum of amount * fee_percentage / 100 over all of that user's completed transactions (status equals "completed"). Transactions with status "pending" or "failed" are ignored.

A country-specific tax rate is applied on top of the base fee:

  • "US" : 10% tax
  • "UK" : 15% tax
  • "IN" : 5% tax
  • Any other country: 0% tax

The final fee for a single transaction is amount * fee_percentage / 100 * (1 + tax_rate).

Return a dictionary mapping each user_id to their total fee, rounded to 2 decimal places. Only include users who have at least one completed transaction.

Function Signature

def calculate_fees(csv_string: str) -> dict[str, float]:

Rules

  • Lines are separated by \n , and the columns appear in the order listed above.
  • The tax rate comes from each transaction's own country , so one user's transactions can be taxed at different rates.
  • Add up a user's unrounded per-transaction fees first, then round that total once to 2 decimal places, as Python's round(total, 2) does.
  • Key order in the returned dictionary does not matter.

Constraints

  • The CSV string is non-empty and always contains a valid header row.
  • There is at least one data row.
  • All amount and fee_percentage values are non-negative numbers.
  • status is one of "completed" , "pending" , "failed" .
  • Country codes are uppercase two-letter strings.
  • user_id values are non-empty strings.
  • The CSV uses commas as delimiters, and no field is quoted or contains a comma.
  • 1 <= number of rows <= 10^4

Examples

Input: csv_string = "user_id,transaction_id,amount,fee_percentage,status,country\nalice,t1,1000,2.5,completed,US\nalice,t2,500,3.0,pending,US\nbob,t3,2000,1.5,completed,UK\nbob,t4,1500,2.0,completed,IN"
Output: {"alice": 27.5, "bob": 66.0}

Alice has one completed transaction, 1000 * 2.5 / 100 * 1.10 = 27.5, and her pending one is ignored. Bob's two completed transactions give 2000 * 1.5 / 100 * 1.15 = 34.5 and 1500 * 2.0 / 100 * 1.05 = 31.5, for a total of 66.0.

Input: csv_string = "user_id,transaction_id,amount,fee_percentage,status,country\ncharlie,t5,800,5.0,failed,DE\ncharlie,t6,1200,3.0,completed,DE\ndave,t7,500,2.0,completed,US"
Output: {"charlie": 36.0, "dave": 11.0}

Charlie's failed transaction is ignored, and his completed one is in DE, which has no tax: 1200 * 3.0 / 100 * 1.0 = 36.0. Dave pays 500 * 2.0 / 100 * 1.10 = 11.0.

Input: csv_string = "user_id,transaction_id,amount,fee_percentage,status,country\nerin,t8,333,0.1,completed,DE\nerin,t9,333,0.1,completed,FR\nerin,t10,333,0.1,completed,BR\nfrank,t11,400,2.0,pending,UK"
Output: {"erin": 1.0}

Each of Erin's transactions costs 333 * 0.1 / 100 = 0.333 with no tax, so her total is 0.999, which rounds to 1.0. Rounding each transaction first would give 0.99. Frank has no completed transaction, so he is left out.