Tiered Subscription Invoice with Mid-Cycle Proration
Arrays & HashingAuto-graded · 19 test casesLast updated
Tests ability to combine prorated flat fees with two-tier volume pricing over a half-open time window.
Problem
You are writing the core calculation for a subscription billing engine. Given a list of usage records and a plan configuration, compute the total amount owed for a billing cycle.
The invoice has two independent components:
1. Flat Subscription Fee (Prorated)
Every subscriber owes a flat base_price for the cycle. If the subscriber enrolled after the cycle started, the flat fee is scaled down proportionally:
prorated_base = base_price × (end_time − enrollment_time) / (end_time − start_time)
A subscriber enrolled exactly at start_time owes the full fee. One enrolled exactly at end_time owes nothing.
2. Volume-Based Usage Charge (Not Prorated)
Count the total units across all usage records whose timestamp falls inside the half-open window [start_time, end_time). Records at exactly end_time belong to the next cycle and must be excluded.
Apply two-tier pricing to the cycle total:
| Units consumed | Rate per unit |
|---|---|
| First 100 units | $0.50 |
| Every unit beyond 100 | $0.25 |
Usage charges are never prorated, regardless of enrollment time.
Final Invoice
invoice = prorated_base + usage_charge
Return the result as a float.
Constraints
1 ≤ end_time − start_time(the billing window always has positive length)start_time ≤ enrollment_time ≤ end_time- All timestamps are non-negative integers
quantityvalues are non-negative integers- Usage records are not guaranteed to be in timestamp order
base_priceis a non-negative float- Records outside
[start_time, end_time)must be ignored
Examples
Example 1
Input: compute_invoice([{'timestamp': 100, 'quantity': 60}, {'timestamp': 200, 'quantity': 60}], {'base_price': 100}, 0, 400, 200)
Output: 105
Example 2
Input: compute_invoice([], {'base_price': 75}, 0, 300, 0)
Output: 75
Example 3
Input: compute_invoice([{'timestamp': 10, 'quantity': 100}], {'base_price': 50}, 0, 100, 0)
Output: 100
Hints
Hint 1
Start by computing the two components — flat fee and usage charge — completely independently, then add them at the end. Mixing them together will cause confusion.
Hint 2
For the tiered usage charge, you only need one number: the total units in the window. Once you have it, `min(total, 100)` gives tier-1 units and `max(0, total - 100)` gives tier-2 units.
Hint 3
The records are unsorted, but order does not matter here — you're computing a single cycle total, not tracking state over time. A single linear pass to filter and sum is all you need.
Worked solution
Hard Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The invoice is the sum of two completely independent calculations. Keeping them separate makes each one straightforward.
Part 1: Prorated flat fee
The subscriber is active from user_join_time to end_time, a span of end_time − user_join_time time units. The full cycle spans end_time − start_time time units. The active fraction is therefore active_length / cycle_length, and multiplying by base_price gives the prorated amount.
Because user_join_time is always in [start_time, end_time], this fraction is always in [0, 1].
Part 2: Tiered usage charge
Ignore record order entirely — tier pricing applies to the cycle total, not to individual records in sequence. One linear pass suffices:
- Filter to records whose timestamp satisfies
start_time <= timestamp < end_time(half-open window;end_timebelongs to the next cycle). - Sum their quantities into
total_units. - Split into tiers with two clamps:
tier1_units = min(total_units, 100)— capped at 100tier2_units = max(0, total_units - 100)— overflow only
Together these partition total_units exactly, with no overlap and no gap.
Why the approach is correct
- Window boundary. Using
< end_time(not<=) correctly excludes records timestamped at the cycle boundary. - Tier boundary. At exactly 100 units,
tier2_units = max(0, 0) = 0, so tier 2 contributes nothing. At 101 units, tier 1 is capped at 100 and tier 2 picks up the one extra unit. - Sorting is unnecessary. Adding numbers is commutative, so the order records arrive in doesn't change the total.
- Proration does not touch usage. The spec is explicit: only the flat fee is scaled; usage charges stand on their own.
Edge cases
| Situation | Result |
|---|---|
| No records in window | total_units = 0, invoice = prorated base only |
Subscriber joins at start_time | Active fraction = 1.0, full base fee charged |
Subscriber joins at end_time | Active fraction = 0.0, base fee = 0.0; usage still billed |
| Exactly 100 units | 100 × $0.50 = $50.00, tier 2 = $0.00 |
Record timestamped at end_time | Excluded from the window |
Complexity
- Time: O(n) — one pass over the
nusage records to filter and sum. - Space: O(1) extra — the generator expression yields values one at a time without building an intermediate list.
Full solution
from typing import List, Dict
def compute_invoice(events: List[Dict], pricing_plan: Dict, start_time: int, end_time: int, user_join_time: int) -> float:
# 1. Prorated flat fee: fraction of the cycle the subscriber was active.
cycle_length = end_time - start_time
active_length = end_time - user_join_time
prorated_base = pricing_plan['base_price'] * active_length / cycle_length
# 2. Sum all units within the half-open window [start_time, end_time).
total_units = sum(
record['quantity']
for record in events
if start_time <= record['timestamp'] < end_time
)
# 3. Apply two-tier pricing to the cycle total.
tier1_units = min(total_units, 100)
tier2_units = max(0, total_units - 100)
usage_charge = tier1_units * 0.50 + tier2_units * 0.25
return prorated_base + usage_charge