Cloud usage bill from regional hourly rates
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked at Stripe. It tests hash map, billing and decimal money.
You are writing the billing step for a cloud platform. A customer's usage report says which region they ran in and how many hours they used each service. A pricing table gives the hourly rate of every service in every region. Compute what the customer owes.
Implement:
def usage_cost(usage_report, pricing_table) -> dict:
...
Each argument is either a parsed JSON object (a dict) or a JSON string.
// usage_report
{
"region": "eu-west-1",
"usage": [
{"service": "vm", "hours": 10},
{"service": "db", "hours": 3.5},
{"service": "vm", "hours": 2.25}
]
}
// pricing_table: region -> service -> hourly rate
{
"eu-west-1": {"vm": "0.12", "db": 0.5},
"ap-south-1": {"vm": "0.10"}
}
Return {"total": "<amount>", "by_service": {"<service>": "<amount>", ...}}. Write amounts as strings with exactly two decimal places.
Part 1: flat hourly rates
- A service can appear on several usage lines. Add its hours together.
- Hours and rates can be integers, decimal numbers, or numeric strings. Hours can be fractional and are billed exactly.
- Money must be exact. Do not do the arithmetic in binary floating point. Round each service's cost half up to the cent, once. The total is the sum of the rounded service lines.
- Raise
ValueErrorif the region has no prices, a service has no price in that region, or hours are negative or not a number.
Part 2: graduated tiers
A service's price can also be a list of tiers in increasing order, for example:
"egress": [{"up_to": 100, "rate": "0.09"}, {"up_to": null, "rate": "0.05"}]
The first 100 hours of that service, in total across all its usage lines, cost 0.09 each. Every hour after that costs 0.05. up_to: null means no upper limit. If the tiers end with a finite up_to and the usage is larger, raise ValueError.
Constraints
- Up to 10^6 usage lines and 10^3 services per region.
Examples
For the report and table above:
{"total": "3.22", "by_service": {"db": "1.75", "vm": "1.47"}}
vm is 12.25 hours x 0.12 = 1.47. db is 3.5 hours x 0.5 = 1.75.
With the egress tiers above added to eu-west-1, a report with two egress lines of 60 and 90 hours costs 100 x 0.09 + 50 x 0.05:
{"total": "11.50", "by_service": {"egress": "11.50"}}
Clarifying questions
- Should a JSON number such as
0.1be read from its text, so it isn't turned into a binary float first? (json.loads(..., parse_float=Decimal)) - Are zero hours allowed, and does a service with zero hours appear in
by_serviceas"0.00"? - In Part 2, are tier limits cumulative upper bounds (
up_to: 100, thenup_to: 250means the next 150 hours)? - Should
by_servicekeys be sorted in the output? - Are booleans or empty strings in the hours field errors?
What a strong answer covers
- Accepts a dict or a JSON string, parses JSON numbers as
Decimal, and converts every hour and rate withDecimal(str(x)), rejecting booleans and non-numeric values. - Adds up hours per service first, then prices each service once and rounds it half up to the cent with
quantize(Decimal("0.01"), ROUND_HALF_UP). The total is the sum of the rounded lines. - For tiers, walks them in order, billing
min(remaining, up_to - previous_up_to)hours at each rate, and raises when hours remain after a finite last tier. - Validates the region, every service's price and negative hours with clear
ValueErrormessages. O(lines + services) time. - Tests: both examples, hours given as strings, a rounding case such as 0.125 to 0.13, a price given as a number, an unknown region and service, a tier boundary hit exactly, and usage past the last finite tier.
Follow-ups
- Tiers apply across all of the customer's regions, not per region.
- Add discounts or free hours per service, and decide whether they apply before or after the tiers.
- Bill several customers from one large file as a stream.
- Rates change during the month at given times.
Where it's asked
What it tests
Sign in to see the worked answer
The full answer, chapter by chapter, with what interviewers expect at each level. New accounts get 5 credits: each one unlocks a question for good.
What the labs asked this week
A weekly email of what OpenAI, Anthropic and the other top AI labs asked engineers, round by round.
Sign in to submit your code; every attempt is kept here.
