Calculate charge with a single price override
Problem Description
You are tasked with calculating the total charge for a customer based on their usage intervals and a single, temporary price override.
Definitions
- Usage Record: An interval
[start, end), wherestartis inclusive andendis exclusive. defaultPrice: The price per unit time (e.g., per day) applied when no override is active.- Price Override: Defined by:
override.period = [overrideStart, overrideEnd): The specific time interval during which the override is active.override.price: The price per unit time applicable during theoverride.period.
Task
Given the following inputs:
usageRecords: []Interval(eachIntervalis[start, end))defaultPrice: intoverride: PriceOverride
Compute the total charge across all usage time. The billing rules are:
- Any portion of usage that overlaps with
override.periodis billed atoverride.price. - Any portion of usage outside
override.periodis billed atdefaultPrice.
The total charge should be returned as an integer.
Notes and Edge Cases
- Usage records may be entirely outside, entirely inside, or partially overlap the override period.
- Assume all inputs are valid (e.g.,
start < end, prices are non-negative). - Crucially, clarify whether
usageRecordscan overlap each other. If they can, your solution must ensure that overlapping usage time is not double-billed (e.g., by first merging overlapping usage intervals).
Example
Consider defaultPrice = 10, an override period [2, 6) with override.price = 5, and a usage record [1, 4).
- The interval
[1, 2)falls outside the override period and is billed at10. - The interval
[2, 4)falls within the override period and is billed at5.
The total charge would be calculated based on these segments.
AI interview prep
Core dilemma
Accurately segmenting time intervals and applying different rates while robustly handling potential overlaps in usage records to prevent double-billing.
3 tradeoffs and 3 stress tests locked
Interview prep
Unlock the interviewer X-Ray rubric, tradeoff matrix, stress tests, clarifying questions, and key concepts.
Calculate charge with a single price override
Problem Description
You are tasked with calculating the total charge for a customer based on their usage intervals and a single, temporary price override.
Definitions
- Usage Record: An interval
[start, end), wherestartis inclusive andendis exclusive. defaultPrice: The price per unit time (e.g., per day) applied when no override is active.- Price Override: Defined by:
override.period = [overrideStart, overrideEnd): The specific time interval during which the override is active.override.price: The price per unit time applicable during theoverride.period.
Task
Given the following inputs:
usageRecords: []Interval(eachIntervalis[start, end))defaultPrice: intoverride: PriceOverride
Compute the total charge across all usage time. The billing rules are:
- Any portion of usage that overlaps with
override.periodis billed atoverride.price. - Any portion of usage outside
override.periodis billed atdefaultPrice.
The total charge should be returned as an integer.
Notes and Edge Cases
- Usage records may be entirely outside, entirely inside, or partially overlap the override period.
- Assume all inputs are valid (e.g.,
start < end, prices are non-negative). - Crucially, clarify whether
usageRecordscan overlap each other. If they can, your solution must ensure that overlapping usage time is not double-billed (e.g., by first merging overlapping usage intervals).
Example
Consider defaultPrice = 10, an override period [2, 6) with override.price = 5, and a usage record [1, 4).
- The interval
[1, 2)falls outside the override period and is billed at10. - The interval
[2, 4)falls within the override period and is billed at5.
The total charge would be calculated based on these segments.