Reconstruct Billing Status
Learn this problemProblem 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
1receives100ad-delivery pennies from the first transaction and40payment pennies from the second transaction. - User
2receives25ad-delivery pennies and10payment 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
7accumulates0 + 15 + 20 = 35ad-delivery pennies and5 + 0 + 30 = 35payment pennies. - User
3accumulates0ad-delivery pennies and12payment 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, andpayment_pennies. user_idis a non-negative integer string.- Each amount field is either an empty string or a non-negative integer string.