PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep

Quick Overview

This question evaluates algorithmic data aggregation, numeric billing computations, and the ability to model proration and threshold semantics across mixed subscription and pay-as-you-go records.

  • hard
  • Stripe
  • Coding & Algorithms
  • Software Engineer

Calculate Per-User Usage Charges

Company: Stripe

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Take-home Project

Implement a billing function for an AI text API. You are given usage records for a single billing cycle. Each record contains: - `user_id` - `input_tokens` - `output_tokens` - `plan`, which is either `PAYG` or `MONTHLY` You are also given a pricing configuration: - `payg_input_rate`: cost per input token under pay-as-you-go - `payg_output_rate`: cost per output token under pay-as-you-go - `monthly_fee`: fixed fee for the monthly plan - `monthly_input_threshold`: number of input tokens included in the monthly plan - `monthly_output_threshold`: number of output tokens included in the monthly plan Billing rules: 1. Under `PAYG`, all input and output tokens are billed at the corresponding per-token rates. 2. Under `MONTHLY`, the user always pays `monthly_fee`. 3. The monthly plan includes up to `monthly_input_threshold` input tokens and `monthly_output_threshold` output tokens. Any usage above those thresholds is billed at the `PAYG` rates for the excess only. 4. A user may have records under both plans in the same billing cycle. For such users, a proration value `p` (where `0 <= p <= 1`) is provided separately, meaning the monthly plan was active for `p` fraction of the cycle. In that case: - charge `p * monthly_fee` - use `p * monthly_input_threshold` and `p * monthly_output_threshold` as the included monthly thresholds - bill all `PAYG` records normally - apply the prorated monthly thresholds only to the user's `MONTHLY` records 5. Aggregate all records by user and return each user's final total charge. Assume there can be up to `10^5` records, so the solution should be efficient.

Quick Answer: This question evaluates algorithmic data aggregation, numeric billing computations, and the ability to model proration and threshold semantics across mixed subscription and pay-as-you-go records.

Implement a billing function for an AI text API. You are given all usage records for one billing cycle. Each record is a tuple of (user_id, input_tokens, output_tokens, plan), where plan is either 'PAYG' or 'MONTHLY'. You are also given pricing values: - payg_input_rate: cost per input token for PAYG usage - payg_output_rate: cost per output token for PAYG usage - monthly_fee: fixed fee for the monthly plan - monthly_input_threshold: included input tokens in the monthly plan - monthly_output_threshold: included output tokens in the monthly plan - prorations: a dictionary mapping user_id to p, where 0 <= p <= 1 Billing rules: 1. PAYG records are always billed at the per-token PAYG rates. 2. If a user has any MONTHLY records, they pay p * monthly_fee, where p is the user's proration value. 3. MONTHLY records share a combined included allowance of p * monthly_input_threshold input tokens and p * monthly_output_threshold output tokens. 4. Only the user's MONTHLY records count against the monthly thresholds. 5. Any MONTHLY usage above the prorated thresholds is billed at the PAYG token rates. 6. If a user has MONTHLY records but is not present in prorations, treat p = 1.0. 7. Return a dictionary mapping each user_id to the user's final total charge, rounded to 6 decimal places. The solution should run efficiently for up to 10^5 records.

Constraints

  • 0 <= len(records) <= 10^5
  • 0 <= input_tokens, output_tokens <= 10^9
  • plan is either 'PAYG' or 'MONTHLY'
  • 0 <= p <= 1 for every proration value
  • If a user has MONTHLY records and is missing from prorations, assume p = 1.0

Examples

Input: ([('alice', 100, 50, 'PAYG'), ('bob', 1200, 900, 'MONTHLY'), ('bob', 100, 50, 'PAYG'), ('carol', 600, 400, 'MONTHLY'), ('carol', 200, 100, 'MONTHLY'), ('carol', 50, 25, 'PAYG')], 0.01, 0.02, 20.0, 1000, 800, {'bob': 1.0, 'carol': 0.5})

Expected Output: {'alice': 2.0, 'bob': 26.0, 'carol': 16.0}

Explanation: alice has only PAYG usage: 100*0.01 + 50*0.02 = 2. bob pays full monthly fee 20, plus excess monthly usage (200 input and 100 output) for 4 more, plus PAYG charges of 2, totaling 26. carol has p=0.5, so monthly fee is 10 and thresholds are 500 input and 400 output; her monthly excess is 300 input and 100 output, adding 5, and her PAYG usage adds 1, so total is 16.

Input: ([], 0.01, 0.02, 10.0, 100, 100, {})

Expected Output: {}

Explanation: No records means no users and no charges.

Input: ([('u1', 50, 50, 'MONTHLY'), ('u1', 50, 50, 'MONTHLY'), ('u1', 10, 0, 'PAYG')], 1.0, 2.0, 30.0, 100, 100, {})

Expected Output: {'u1': 40.0}

Explanation: u1 is not in prorations, so p=1.0. The two MONTHLY records exactly match the included thresholds, so only the monthly fee of 30 is charged for them. The PAYG record adds 10*1.0 = 10, for a total of 40.

Input: ([('z', 10, 10, 'MONTHLY'), ('z', 5, 5, 'PAYG')], 2.0, 3.0, 100.0, 100, 100, {'z': 0.0})

Expected Output: {'z': 75.0}

Explanation: With p=0, the monthly fee and included thresholds are both zero. So the MONTHLY usage is fully billed at PAYG rates: 10*2 + 10*3 = 50. The PAYG record adds 5*2 + 5*3 = 25, for a total of 75.

Input: ([('m', 30, 40, 'MONTHLY'), ('n', 1, 2, 'PAYG')], 1.0, 1.5, 5.0, 100, 100, {})

Expected Output: {'m': 5.0, 'n': 4.0}

Explanation: m has monthly usage below the included thresholds, so m pays only the monthly fee of 5. n has only PAYG usage: 1*1.0 + 2*1.5 = 4.

Hints

  1. Aggregate data by user. PAYG charges can be added immediately, but MONTHLY usage should be summed first.
  2. For each user with MONTHLY usage, apply the prorated fee and prorated thresholds once to that user's total MONTHLY input and output tokens.
Last updated: May 24, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,000+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Assign Reviewers from Changed Files - Stripe (medium)
  • Generate Account Email Notifications - Stripe (medium)
  • Calculate Transaction Fees - Stripe (medium)
  • Build an Account Transfer Ledger - Stripe (medium)
  • Implement Validation and String Compression - Stripe (hard)