Voucher Exchange Network
Problem
An event operator accepts several partner-issued vouchers. Each published exchange rate is directional: a counter may exchange one BRONZE voucher for three SILVER vouchers without offering the reverse exchange. A caller already knows which counters to visit and supplies that route.
Design VoucherExchangeNetwork to store the fixed directed rates, calculate the combined multiplier for a route, and convert an amount through that route. Calls are sequential on one shared instance.
Requirements
- Indexed directed lanes. Construction assigns
multipliers[i]to the ordered pair(fromVoucherIds[i], toVoucherIds[i]). - Exact lane lookup.
hasLane(fromVoucherId, toVoucherId)reports whether that ordered pair exists. - Route composition.
routeMultiplier(route)returns the product of the multipliers for every adjacent directed lane inroute. - Amount exchange.
exchange(amount, route)returnsamountmultiplied by the route multiplier. - Immutable chart. Every public method preserves the lane mapping created by construction.
API
| Signature | Returns | Behavior |
|---|---|---|
VoucherExchangeNetwork(fromVoucherIds: string[], toVoucherIds: string[], multipliers: integer[]) | Not applicable | Performs indexed lane construction. |
hasLane(fromVoucherId: string, toVoucherId: string) | boolean | Evaluates exact lane lookup. |
routeMultiplier(route: string[]) | integer | Performs route composition. |
exchange(amount: integer, route: string[]) | integer | Performs amount exchange. |
Examples
Starting with exchanges BRONZE -> SILVER at 3 and SILVER -> GOLD at 4:
| Step | Operation | Result |
|---|---|---|
| 1 | hasLane("BRONZE", "SILVER") | true |
| 2 | hasLane("SILVER", "BRONZE") | false |
| 3 | routeMultiplier(["BRONZE", "SILVER", "GOLD"]) | 12 |
| 4 | exchange(5, ["BRONZE", "SILVER", "GOLD"]) | 60 |
Constraints
- The three constructor arrays have equal lengths from 1 through 20.
- Each paired voucher ID differs, matches
[A-Za-z0-9_-]{1,24}, and every directed pair is unique. - Each multiplier is an integer from 1 through 10.
- Every route contains 2 through 6 voucher IDs, and every adjacent directed lane exists.
- Each amount is an integer from 1 through 1000.
- Every route product and exchange result fits a signed 32-bit integer.
- A testcase contains at most 50 public method calls.
Notes
All calls satisfy the constraints. Reverse lanes are not inferred, and routes are evaluated in caller-supplied order. Behavior outside the preconditions is unspecified and is not judged.