FastPrepFastPrep
Problem Brief

Chat Billing Calculation

OA

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.

1Example 1

Input
sessions = ["userA,100,120,payg","userB,150,100,payg","userB,100,130,payg"]
Output
["userA: $0.07","userB: $0.14"]
Explanation
userA: (100/100 * $0.03) + (120/100 blocks = 1 block * $0.04) = $0.03 + $0.04 = $0.07. userB session 1: (1 block input * $0.03) + (1 block output * $0.04) = $0.07. userB session 2: (1 block input * $0.03) + (1 block output * $0.04) = $0.07. userB total = $0.14.

2Example 2

Input
sessions = ["userA,100,100,payg","userB,20000,10000,fixed","userB,25000,12000,fixed"]
Output
["userA: $0.07","userB: $17.30"]
Explanation
userA payg: $0.03 + $0.04 = $0.07. userB fixed: flat fee $15.00 + overage on input (45000 - 40000 = 5000 tokens = 50 blocks * $0.03/block... wait, rate is $0.03 per 100 tokens so 5000 * 0.03 / 100 = $1.50) + overage on output (22000 - 20000 = 2000 tokens, 2000 * 0.04 / 100 = $0.80). Total = 15.00 + 1.50 + 0.80 = $17.30.

3Example 3

Input
sessions = ["userA,100,100,payg","userA,100,100,payg","userA,20000,10000,fixed","userA,100,100,fixed","userB,100,100,payg"]
Output
["userA: $7.71","userB: $0.07"]
Explanation
userA has 4 sessions total: 2 payg + 2 fixed. Two payg sessions: 2 * ($0.03 + $0.04) = $0.14. Fixed plan prorated to 2/4 = 50%: fee = $7.50, allowances = 20000 input + 10000 output. Fixed sessions usage: 20100 input (overage 100 = 1 block * $0.03 = $0.03) + 10100 output (overage 100 = 1 block * $0.04 = $0.04) = $0.07. Total: 0.14 + 7.50 + 0.07 = $7.71. userB: $0.07.
public List<String> calculateMonthlyBilling(List<String> sessions) {
  // write your code here
}
Input

sessions

["userA,100,120,payg","userB,150,100,payg","userB,100,130,payg"]

Output

["userA: $0.07","userB: $0.14"]

Sign in to submit your solution.