Drill

ProblemsC# › payments

How much of this can still be refunded

easypaymentsMathC#

An agent asks to refund an amount against an order that may already have been partly refunded.

RefundableAmount(paid: int, refunded: int, requested: int) → int

C# needs a compiler and Drill does not host one yet, so this page is the reference rather than an exercise: the problem, worked examples, and the solution in full. To type it out, the same problem runs in Python.

Solve it in Python →

Where you start

public int RefundableAmount(int paid, int refunded, int requested) {
    
}

Worked examples

CallResult
RefundableAmount(1000, 200, 500)500
RefundableAmount(1000, 200, 900)800
RefundableAmount(1000, 1000, 50)0
RefundableAmount(1000, 0, -5)0

Hint

Work out what remains, then take the smaller of that and the request — floored at zero.

Reference solution in C#
public int RefundableAmount(int paid, int refunded, int requested) {
    if (requested <= 0) return 0;
    int left = paid - refunded;
    if (left <= 0) return 0;
    return Math.Min(left, requested);
}

The same problem in another language

More payments problems in C#