Your question is Chronological Points Subtraction. Start with the requirements on the right.
Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.
Fetch receives point transactions for users. Positive transactions award points, while negative transactions redeem points. Implement a function that processes every transaction in chronological order and applies redemptions to the user's oldest available point awards first.
Transactions may be provided in any order. If two transactions have the same timestamp, process them in their original input order. A redemption may consume points from multiple awards, and any partially consumed award remains available for future redemptions. Every redemption is guaranteed to be no greater than the user's available balance at that point in chronological processing.
Implement calculate_balances(transactions), where transactions is a list of dictionaries with:
user: a non-empty stringpoints: a nonzero integer, positive for an award and negative for a redemptiontimestamp: an integer used for chronological orderingReturn a dictionary mapping each user to their final nonnegative integer balance. Do not mutate the input list.
def calculate_balances(transactions):