Level: Mid-Level
Round: Online Assessment · Type: Coding · Difficulty: 7/10 · Duration: 60 min · Interviewer: Neutral
Topics: Billing Systems, String Processing, Data Aggregation, Conditional Logic
Location: San Francisco Bay Area
Interview date: 2026-03-21
This online assessment involved calculating monthly billing for a chat-based AI platform, considering pay-as-you-go and fixed pricing plans. The solution required handling token usage, plan switching, and prorated fees.
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 calculate_monthly_billing 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)
We will start by implementing 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 0–99 → 0 blocks 100–199 → 1 block 200–299 → 2 blocks A user with no full blocks should still appear with $0.00 Output should be sorted alphabetically by user_id Example:
` Input: [ "userA,100,120,payg", "userB,150,100,payg", "userB,100,130,payg" ]
Output: [ "userA: $0.07", "userB: $0.14" ] `
Requirement 2: Fixed pricing (test cases 5–9)
Extend the function to support fixed monthly plan (plan: "fixed")
Fixed plan: Flat fee: $15.00 per month Includes: 40,000 input tokens 20,000 output tokens Overage is charged using pay-as-you-go rates Example:
` Input:
[ "userA,100,100,payg", "userB,20000,10000,fixed", "userB,25000,12000,fixed" ]
Output:
[ "userA: $0.07", "userB: $17.30" ]
User B calculation:
15.00 + (5000 * 0.03 / 100) + (2000 * 0.04 / 100) `
Requirement 3: Plan switching (test cases 10–13)
Users can switch plans during a billing cycle.
Fixed plan fee and allowances are prorated by number of sessions Example: 2 payg sessions 2 fixed sessions → fixed plan counts as 50% Important: Token usage blocks are calculated per plan separately
Example:
` Input: [ "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" ] ` User A calculation: Two payg sessions = $0.14
Monthly fee = 15 * (2/4) = $7.50
Allowances: 40000 * (2/4) = 20000 20000 * (2/4) = 10000
Overage = $0.07
Total = 0.14 + 7.50 + 0.07 = 7.71