Payment Preference Resolver
Problem
Design PaymentPreferenceResolver to choose a payment type by combining its supported transaction amount with the preference order configured for a transaction category.
Requirements
- Amount eligibility. A payment type is eligible exactly when the transaction amount is less than or equal to its registered inclusive maximum.
- Category preferences.
defineCategoryPreferencestores the supplied payment types from most to least preferred for that category. - Preferred eligible type. Selection returns the first eligible type in the category's configured order.
- No eligible type. Selection returns the empty string when every configured type is excluded.
API
| Signature | Returns | Behavior |
|---|---|---|
PaymentPreferenceResolver() | Not applicable | Creates an empty resolver. |
addPaymentType(paymentType: string, maximumAmount: integer) | void | Adds one payment type and its amount limit. |
defineCategoryPreference(category: string, paymentTypes: string[]) | void | Adds one category and its preference order. |
selectPaymentType(amount: integer, category: string) | string | Resolves one payment type for the amount and category. |
Examples
| Step | Operation | Result |
|---|---|---|
| 1 | addPaymentType("UPI", 2000) | void |
| 2 | addPaymentType("NET_BANKING", 10000) | void |
| 3 | defineCategoryPreference("GROCERIES", ["UPI", "NET_BANKING"]) | void |
| 4 | defineCategoryPreference("TRAVEL", ["NET_BANKING", "UPI"]) | void |
| 5 | selectPaymentType(1500, "GROCERIES") | "UPI" |
| 6 | selectPaymentType(5000, "GROCERIES") | "NET_BANKING" |
| 7 | selectPaymentType(1500, "TRAVEL") | "NET_BANKING" |
| 8 | selectPaymentType(15000, "TRAVEL") | "" |
Constraints
- At most
100payment types,1000categories, and100000total method calls occur per testcase. - Payment type and category names contain from
1through64Unicode code points and are compared exactly. - Amounts and maximum amounts are from
1through9000000000000000000. - A payment type is added once. A category is defined once, after all payment types are added.
- Each category preference contains every registered payment type exactly once.
- Selection uses a defined category. Inputs outside these preconditions are not judged.
Notes
Payment type and category names use the exact strings supplied during configuration. Calls are sequential, and concurrent access is outside scope.