Compute Platform Balance with Optional Destinations
Problem statement
Process a batch of charges and return the final amount retained by the platform.
Each charge is one comma-separated string with exactly three fields:
amount,destination_account,destination_amountamount is the positive charge amount. The two destination fields are optional and may be empty.
- If both destination fields are empty, the platform retains the full charge amount.
- If
destination_accountis present anddestination_amountis empty, the full charge amount is transferred away, so the platform retains zero. - If
destination_amountis present, subtract that amount from the charge before adding the remainder to the platform balance. This rule also applies whendestination_accountis empty.
Return the sum retained by the platform across all charges.
What the interview report shared
The reported OA used optional destination-account and destination-amount parameters in a Platform Balance task. The candidate reasoned about three hidden cases: missing account, account without amount, and amount without account. Those diagnoses were tentative rather than confirmed rules.
Function
calculatePlatformBalance(charges: String[]) → longExamples
Example 1
charges = ["100,,","80,shop_a,","120,shop_b,50","90,,20"]return = 240The retained amounts are 100, 0, 70, and 70. Their sum is 240.
Example 2
charges = ["25,merchant_1,5","40,merchant_2,40","10,,0"]return = 30The platform retains 20, 0, and 10.
Constraints
1 <= charges.length <= 10^5.1 <= amount <= 10^9.- When present,
0 <= destination_amount <= amount. - A destination account contains lowercase English letters, digits, or underscores.
- The final answer fits in a signed 64-bit integer.