Chat Billing Calculation
Learn this problemProblem statement
You are building a billing component for a chat-based AI platform that charges users based on token usage.
Each chat session generates a record containing: user_id,input_tokens,output_tokens,plan
You are given a list of chat session records for a single month. A user may appear multiple times in the list, representing multiple chat sessions during the month.
You will implement the function calculateMonthlyBilling that takes the list of chat sessions, and returns a list of strings representing each user's total spend for that month, in the format user_id: $xx.xx.
Requirement 1: Pay as you go pricing (test cases 0–4)
Pay-as-you-go pricing (plan: "payg"):
- Input tokens are billed at $0.03 per 100 tokens
- Output tokens are billed at $0.04 per 100 tokens
Notes:
- Token counts will always be non-negative integers
- Billing is done in blocks of 100 tokens. Any partial block is not charged. For example, 0–99 tokens → 0 blocks billed, 100–199 tokens → 1 block billed, 200–299 tokens → 2 blocks billed, etc.
- A user that has no full blocks (i.e. uses less than 100 tokens per session) should still be in the output array with $0.00 as their spend
- The output array should be ordered alphabetically by user_id
Requirement 2: Fixed pricing (test cases 5–9)
Extend the function to calculate spend for the fixed monthly plan (plan: "fixed"):
- Flat fee: $15.00 per month
- Includes: 40,000 input tokens and 20,000 output tokens
- Any usage above the included tokens ("overage") is charged at the pay-as-you-go rates
Requirement 3: Plan switching (test cases 10–13)
Users are able to switch plans during a billing cycle. If a user switches plans, prorate the fixed plan fee and allowances by number of sessions. For example, if the user had 2 sessions on "pay-as-you-go", and 2 sessions on "fixed" (4 sessions total), their $15 fee and allowances for the fixed sessions would be reduced by 50%. Note: token usage blocks should be calculated per plan, and costed separately.
Function
calculateMonthlyBilling(sessions: List<String>) → List<String>Examples
Example 1
sessions = ["userA,100,120,payg","userB,150,100,payg","userB,100,130,payg"]return = ["userA: $0.07","userB: $0.14"]Example 2
sessions = ["userA,100,100,payg","userB,20000,10000,fixed","userB,25000,12000,fixed"]return = ["userA: $0.07","userB: $17.30"]Example 3
sessions = ["userA,100,100,payg","userA,100,100,payg","userA,20000,10000,fixed","userA,100,100,fixed","userB,100,100,payg"]return = ["userA: $7.71","userB: $0.07"]More Stripe problems
- Group Linked Merchant RecordsPHONE SCREEN · Seen Jul 2026
- Invoice / Payment ReconciliationPHONE SCREEN · Seen Jul 2026
- Incident MonitorOA · Seen Jul 2026
- Merchant Fraud Risk ScoringOA · Seen Jul 2026
- WebSocket Load Balancer — Basic Load Balancing (Part 1)OA · Seen Jul 2026
- Rule-Driven Transaction Fraud DetectionOA · PHONE SCREEN · Seen Jul 2026
- Deployment Window SchedulerOA · Seen Jul 2026
- Directly Linked UsersOA · Seen Jun 2026