Drill

ProblemsGo › payments

How much of this can still be refunded

easypaymentsMathGo

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

Go 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

func refundableAmount(paid int, refunded int, requested int) int {
	
}

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 Go
func refundableAmount(paid int, refunded int, requested int) int {
	if requested <= 0 {
		return 0
	}
	left := paid - refunded
	if left <= 0 {
		return 0
	}
	if requested < left {
		return requested
	}
	return left
}

The same problem in another language

More payments problems in Go