Problem Β· Array

Reconstruct Billing Status

Learn this problem
● EasyRedditONSITE INTERVIEW

Problem statement

πŸ₯ Practice note: This version should match about 80%-85% of the reported Reddit onsite question. The core part to practice is rebuilding each advertiser's billing status from transaction logs.

πŸ“ FastPrep format note: The original prompt described Transaction objects and a Record output, but did not include FastPrep-style sample tests. To make this page runnable, the examples below use a small wrapper format: each transaction is encoded as [user_id, ad_delivery_pennies, payment_pennies], an empty string means the amount field is missing, and the return value is sorted by user_id as "user_id:ad_delivery_pennies,payment_pennies".

Reddit stores billing statuses for advertisers in a database. Due to a data issue, the stored billing statuses were deleted. However, the system still has the complete transaction log.

Your task is to rebuild each advertiser's BillingStatus from the transaction logs.

Each user's billing status starts with two counters:

ad_delivery_pennies = 0
payment_pennies = 0

You are given a list of transaction logs. Each transaction belongs to exactly one user and contains a user_id. A transaction may include one or more billing amount fields, such as ad_delivery_pennies or payment_pennies.

For each transaction, update the billing status of the corresponding user by adding every amount field present in that transaction. If a field is missing from a transaction, treat it as 0.

Return the final billing status for every user that appears in the transaction logs.

Original Function Signature

function reconstructBillingStatus(transactions: Transaction[]): Record<number, BillingStatus>

Original Input Shape

Transaction {
  user_id: number
  ad_delivery_pennies?: number
  payment_pennies?: number
}

Original Output Shape

BillingStatus {
  ad_delivery_pennies: number
  payment_pennies: number
}

Return a mapping from user_id to BillingStatus.

Function

reconstructBillingStatus(transactions: String[][]) β†’ String[]

Examples

Example 1

transactions = [["1", "100", ""], ["1", "", "40"], ["2", "25", "10"]]return = ["1:100,40", "2:25,10"]

This is a FastPrep practice example for the wrapper format.

  • User 1 receives 100 ad-delivery pennies from the first transaction and 40 payment pennies from the second transaction.
  • User 2 receives 25 ad-delivery pennies and 10 payment pennies.

The result is returned in increasing user_id order.

Example 2

transactions = [["7", "", "5"], ["7", "15", ""], ["7", "20", "30"], ["3", "", "12"]]return = ["3:0,12", "7:35,35"]

This is a FastPrep practice example for the wrapper format.

  • User 7 accumulates 0 + 15 + 20 = 35 ad-delivery pennies and 5 + 0 + 30 = 35 payment pennies.
  • User 3 accumulates 0 ad-delivery pennies and 12 payment pennies.

Missing amount fields are treated as 0, and the result is sorted by numeric user_id.

Constraints

FastPrep wrapper note: These constraints describe the runnable wrapper used on this page, not extra details from the original interview prompt.

  • transactions.length >= 1
  • Each row has exactly three strings: user_id, ad_delivery_pennies, and payment_pennies.
  • user_id is a non-negative integer string.
  • Each amount field is either an empty string or a non-negative integer string.
drafts saved locally
public String[] reconstructBillingStatus(String[][] transactions) {
  // write your code here
}
transactions[["1", "100", ""], ["1", "", "40"], ["2", "25", "10"]]
expected["1:100", "40", "2:25", "10"]
checking account