Capital Loans — Withholding and Direct Repayment
Problem statement
Process a merchant's loans in creation order. Every loan has principal, a fee, and a percentage of later sales that should be withheld for repayment.
Each operation has one of these forms:
CREATE loanId principal fee percentPROCESS saleAmountPAY amount
CREATEappends a new loan. Loan IDs are unique.PROCESSlooks only at the oldest loan that still has principal or fee outstanding. Withholdfloor(saleAmount * percent / 100), cap it at that loan's total outstanding amount, and apply it to that loan. Any unused withheld amount is discarded instead of moving to a later loan.PAYapplies the supplied amount across as many outstanding loans as needed, oldest first. Discard any excess after every loan is paid.- For every repayment, pay the loan's remaining fee before its principal.
Return two values: the total principal still outstanding across all loans, and the total fee amount actually received.
What the interview report shared
The source described separate loans with individual repayment percentages, sale withholding against at most one oldest loan, direct payments across several oldest loans, fee-before-principal allocation, discarded excess, and output covering debt and received fees.
Function
summarizeCapitalLoans(operations: String[]) → long[]Examples
Example 1
operations = ["CREATE A 1000 100 20", "PROCESS 500", "PAY 450"]return = [550, 100]The sale withholds 100, which pays loan A's fee. The direct payment then reduces its principal from 1000 to 550. A total of 100 in fees was received.
Example 2
operations = ["CREATE A 100 20 50", "CREATE B 200 30 10", "PROCESS 400", "PAY 170"]return = [60, 50]The sale withholds 200 but loan A needs only 120; the unused 80 is discarded. The direct payment then pays B's 30 fee and 140 principal, leaving 60 principal on B. Across both loans, 60 principal remains and 50 in fees was received.
Constraints
1 <= operations.length <= 10^5.- Principal, fee, sale amounts, direct payments, and every intermediate product fit in signed 64-bit arithmetic.
0 <= percent <= 100.- Every
CREATEuses a unique loan ID. - For this exercise, assume percentage withholding uses floor division, invalid operations do not occur, and repayments cover fee before principal.