FastPrepCalculate a Discount

Calculate a Discount

Stripe logoStripe● EasyINTERNPHONE SCREEN
Learn

Problem statement

Given a nonnegative monetary amount amountCents and a percentage discount discountBasisPoints, calculate the discount amount in whole cents.

One basis point is 0.01%, so 10000 basis points represents 100%. Return floor(amountCents * discountBasisPoints / 10000).

Return the savings, rather than the remaining price. Round down once after multiplying; no eligibility, stacking, or stateful rules apply.

Function

calculateDiscount(amountCents: long, discountBasisPoints: int) → long

Examples

Example 1

amountCents = 1000discountBasisPoints = 1500return = 150

1500 basis points is 15%. The discount is 1000 * 1500 / 10000 = 150 cents.

Example 2

amountCents = 101discountBasisPoints = 3333return = 33

The exact savings is 33.6633 cents. Rounding down once gives 33 cents.

Example 3

amountCents = 1000000000000discountBasisPoints = 10000return = 1000000000000

A 100% discount equals the original amount. The product 10000000000000000 fits signed 64-bit arithmetic.

Constraints

  • 0 <= amountCents <= 1000000000000.
  • 0 <= discountBasisPoints <= 10000.
  • Inputs are integers. Use signed 64-bit arithmetic for monetary values and intermediate products.

More Stripe problems

See Stripe hiring insights
public long calculateDiscount(long amountCents, int discountBasisPoints) {
    // Write your code here.
}
amountCents1000
discountBasisPoints1500
expected150
Checking account…