Cashback Tracker
Updated
Problem statement
One customer uses a credit card that pays cashback on each purchase. Write a tracker that keeps this cashback up to date and answers two questions: the customer's total cashback, and which purchases of a given month have the most cashback left.
A purchase of dollars whole dollars at a rate of percent% earns dollars × percent cents. A refund hands back part or all of one purchase. The purchase keeps its rate and its date, and its cashback becomes (dollars it still keeps) × percent cents. The same purchase may be refunded several times, until nothing of it is left.
The total is the cashback of every purchase so far, from any month, after refunds. The top list for a month and a number n holds at most n ids. They come from the purchases dated in that month whose cashback is still above 0, taken in order of the pair (cashback left, id): higher cashback first and, for equal cashback, lower id first.
Each string of calls is one of "PURCHASE id date dollars percent", "REFUND id dollars", "TOTAL" and "TOP month n", where date looks like YYYY-MM-DD and month like YYYY-MM. Handle them in array order. A TOTAL call adds the row [total] to the returned array, and a TOP call adds its list of ids as a row, possibly empty. Purchases and refunds add no row.
Function
trackCashback(calls: String[]) → int[][]Examples
Example 1
calls = ["PURCHASE 31 2025-03-04 80 2","PURCHASE 7 2025-04-19 250 1","PURCHASE 12 2025-03-28 45 5","PURCHASE 5 2025-03-11 120 1","TOTAL","TOP 2025-03 2"]return = [[755],[12,31]]- Each purchase earns dollars times percent in cents: purchase
31earns80 × 2 = 160, purchase7earns250 × 1 = 250, purchase12earns45 × 5 = 225, and purchase5earns120 × 1 = 120. TOTALcounts every month, so it gives160 + 250 + 225 + 120 = 755.TOP 2025-03 2only looks at the March 2025 purchases31,12and5; purchase7is from April. Their cashback is160,225and120, and the two largest belong to12and then31.
Example 2
calls = ["PURCHASE 3 2024-11-02 200 3","PURCHASE 8 2024-11-15 150 5","PURCHASE 4 2024-11-30 90 4","TOP 2024-11 3","REFUND 8 70","REFUND 4 90","TOP 2024-11 3","TOTAL","TOP 2024-12 1"]return = [[8,3,4],[3,8],[1000],[]]- Before any refund, purchase
3has200 × 3 = 600cents, purchase8has150 × 5 = 750and purchase4has90 × 4 = 360, so the firstTOPgives[8,3,4]. - Purchase
8gets 70 of its 150 dollars back. The 80 dollars it keeps still earn 5%, so it now has80 × 5 = 400cents. It lost70 × 5 = 350cents, not 70. - Purchase
4gets all 90 dollars back, so it has 0 cents and is left out of laterTOProws. - Now
600 > 400, so the secondTOPgives[3,8]: only two ids, althoughn = 3.TOTALgives600 + 400 + 0 = 1000. - Nothing was bought in December 2024, so
TOP 2024-12 1gives the empty row[].
Example 3
calls = ["PURCHASE 52 2027-06-03 90 2","PURCHASE 8 2027-06-11 36 5","PURCHASE 60 2027-06-20 50 4","PURCHASE 23 2027-06-27 70 1","REFUND 60 5","TOP 2027-06 4","TOTAL"]return = [[8,52,60,23],[610]]- Purchase
52has90 × 2 = 180cents, purchase8has36 × 5 = 180, purchase60has50 × 4 = 200and purchase23has70 × 1 = 70. - Refunding 5 dollars of purchase
60leaves 45 dollars, so it falls from 200 to45 × 4 = 180and joins52and8at 180. - Inside that group of three, ids run from low to high:
8, then52, then60. So8leads although52was bought before it, and60, which led before the refund, is now last of the three. Purchase23follows with 70, andn = 4keeps all four:[8,52,60,23]. TOTALgives180 + 180 + 180 + 70 = 610.
Constraints
1 <= calls.length <= 2000- Every string in
callsis aPURCHASE,REFUND,TOTALorTOPcall written as in the statement, with one space between parts; ids, dollars, percents andnare positive whole numbers whose first digit is not 0. - Each
idlies between1and10^9, and no twoPURCHASEcalls use the sameid. - Every
dateis a real calendar date from2020-01-01to2029-12-31, and everymonthis from2020-01to2029-12. - In a
PURCHASE,1 <= dollars <= 10^5and1 <= percent <= 5. - A
REFUNDnames theidof an earlierPURCHASE, and itsdollarsis at least1and at most the part of that purchase not refunded yet. - In a
TOP,1 <= n <= 2000. - At least one call is
TOTALorTOP.
More Capital One Onsite Interview questions
- Digit-Coded Card ValidationONSITE INTERVIEW · Seen Nov 2026
- Sort ColorsONSITE INTERVIEW · Seen Oct 2026
- Virtual Card Transaction ValidatorONSITE INTERVIEW · Seen Oct 2026
- Banking System with Top ActivityONSITE INTERVIEW · Seen Sep 2026
- Transaction Event StoreONSITE INTERVIEW · Seen Jul 2026
- Shortest Path in an Unweighted GraphONSITE INTERVIEW · Seen Jul 2026
- Duplicate Transactions Within 60 SecondsONSITE INTERVIEW · Seen May 2026
- LRU CachePHONE SCREEN · ONSITE INTERVIEW · Seen May 2026
- Reverse a StringOA · ONSITE INTERVIEW · Seen Oct 2025
- Most Cost-Effective MachineONSITE INTERVIEW · Seen Sep 2025
Similar questions at other companies
- In-Memory Filesystem CommandsPerplexity AI · PHONE SCREEN · ONSITE INTERVIEWPerplexity AI interview questions
- Camel CardsRippling · PHONE SCREEN · ONSITE INTERVIEWRippling interview questions
- Calendar Event System with Pagination and IntersectionLead Bank · ONSITE INTERVIEWLead Bank interview questions
- CD Rental SystemOkta · ONSITE INTERVIEWOkta interview questions
- Order Manager with Partial Cancellations and Indexed QueriesStripe · ONSITE INTERVIEWStripe interview questions
- Set Command SimulationPure Storage · ONSITE INTERVIEWPure Storage interview questions
- Worker Management with Promotions and Double-Paid TimeInstacart · ONSITE INTERVIEWInstacart interview questions
- Banking System with Pending Transfer AcceptanceCircle · PHONE SCREENCircle interview questions
- Streaming Top-K Frequent ElementsNVIDIA · PHONE SCREENNVIDIA interview questions
- Temporal Key-Field Database with Prefix ScansThe Trade Desk · OA
Capital One interview experiences
- Capital One Experienced Engineer Power Day and OA: 6 Experiences
- Capital One New Grad OA Questions: 14 Experiences (Part 2 of 2)
- Capital One TDP Agentic Round (AI Coding): 7 Experiences
- Capital One SWE Power Day, Level Not Reported: 6 Experiences
- Capital One TIP Intern OA and Power Day: 13 Experiences
- Capital One TDP Agentic Power Day: 25 Experiences (Part 1 of 2)