Loading...
Loading...
You are building a billing analytics system for a SaaS platform. The system receives a stream of billing events, where each event is either a charge (money collected from an account) or a refund (money returned to an account). Your task is to process these events and compute the net revenue for each account.
You are given a list of billing events. Each event is represented as a tuple/list of three elements:
account_id (string): unique identifier for the accountevent_type (string): either "charge" or "refund"amount (integer): the dollar amount of the event (always positive)Return a dictionary mapping each account_id to its net revenue, where:
charge increases the net revenuerefund decreases the net revenueThe result should include all accounts that appear in the events. Accounts with a net revenue of zero should still be included.
Return a dictionary { account_id: net_revenue } sorted by account_id alphabetically (or return as a regular dict — order does not matter for correctness).
Input:
events = [
["acct_A", "charge", 100],
["acct_B", "charge", 200],
["acct_A", "refund", 30]
]
Output: {"acct_A": 70, "acct_B": 200}
Explanation: acct_A had a charge of 100 and a refund of 30, so net = 70. acct_B had only a charge of 200.
Input:
events = [
["acct_X", "charge", 500],
["acct_X", "refund", 500]
]
Output: {"acct_X": 0}
Explanation: The charge and refund cancel out, resulting in a net revenue of 0. The account still appears in the output.
Input:
events = []
Output: {}
Explanation: No events means no accounts and an empty result.
n is the number of events.0 <= len(events) <= 10^5 account_id consists of lowercase letters, digits, and underscores event_type is either "charge" or "refund" 1 <= amount <= 10^6 An account may appear in multiple events of any type