Implement a request-budget report with explicit empty-data behavior.

Instruction: Write the function and show the expected report for the supplied counts.

Context:

Implement Python budget_report(eligible, bad, slo). Counts must be integers excluding booleans, with 0 ≤ bad ≤ eligible. slo must be a finite decimal string strictly between zero and one. Return availability, allowed_bad, remaining_bad and consumed_fraction using Decimal values; availability and consumed_fraction are None when eligible is zero. Preserve a negative remaining budget. Example: eligible=100000, bad=150, slo="0.999". This reports one supplied window; it is not an alert engine.

Updated

Example Answer

I would validate counts before doing arithmetic and parse the objective from its decimal string, avoiding a binary-float input. Empty traffic should produce an undefined availability and consumption ratio, not a false 100% result. A negative remaining allowance is useful evidence of an exhausted budget, so I would not clamp it to zero.

For the example, the allowance is 100 bad requests, 150 have occurred, and the remaining allowance is minus 50. Availability is 99.85%, and consumption is 150%.

Worked reasoning

from decimal import Decimal, InvalidOperation


def budget_report(eligible, bad, slo):
    if type(eligible) is not int or type(bad) is not int:
        raise ValueError("counts must be integers, not booleans")
    if eligible < 0 or bad < 0 or bad > eligible:
        raise ValueError("require 0 <= bad <= eligible")
    if not isinstance(slo, str):
        raise ValueError("slo must be a decimal string")
    try:
        target = Decimal(slo)
    except InvalidOperation:
        raise ValueError("invalid slo") from None
    if not target.is_finite() or not Decimal(0) < target < Decimal(1):
        raise ValueError("require a finite 0 < slo < 1")
    allowed = Decimal(eligible) * (Decimal(1) - target)
    return {
        "availability": Decimal(eligible - bad) / Decimal(eligible)
                        if eligible else None,
        "allowed_bad": allowed,
        "remaining_bad": allowed - Decimal(bad),
        "consumed_fraction": Decimal(bad) / allowed if eligible else None,
    }


report = budget_report(100000, 150, "0.999")
assert report["availability"] == Decimal("0.9985")
assert report["allowed_bad"] == Decimal("100")
assert report["remaining_bad"] == Decimal("-50")
assert report["consumed_fraction"] == Decimal("1.5")

Exercise zero traffic, zero failures, all failures, an exhausted budget, negative counts, bad greater than eligible, booleans, NaN, infinity and boundary objectives 0 and 1. The function assumes the caller's counts cover the same window and classification. Decimal division uses the active precision; format rounded display values separately from the report.

What the interviewer is assessing

Core checks (1 point each):

  • Validates count types, ranges and finite objective bounds.
  • Returns undefined ratios for empty traffic and preserves negative remaining budget.
  • Computes 99.85% availability, 100 allowed, −50 remaining and 150% consumption.

Stronger (+1 after all core checks): Separates measurement coverage and display rounding from the arithmetic function.

Acceptable alternatives: An exact rational implementation is acceptable if it preserves the same contract and rejects nonfinite inputs.

Red flags: Clamps exhausted budgets to zero, counts booleans as integers or calls no traffic perfect availability.

Follow-up

Can this function decide whether to page the on-call engineer?

Example response: Not alone. Paging needs a documented policy, time windows, traffic sufficiency, telemetry health and an actionable response. This function only computes a report from supplied counts; missing or biased counts remain an upstream problem.

Related Questions