Problemcurrency conversion network

Session: Sign in to solve

Solution.txt

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 in route.
  • Amount exchange. exchange(amount, route) returns amount multiplied by the route multiplier.
  • Immutable chart. Every public method preserves the lane mapping created by construction.

API

SignatureReturnsBehavior
VoucherExchangeNetwork(fromVoucherIds: string[], toVoucherIds: string[], multipliers: integer[])Not applicablePerforms indexed lane construction.
hasLane(fromVoucherId: string, toVoucherId: string)booleanEvaluates exact lane lookup.
routeMultiplier(route: string[])integerPerforms route composition.
exchange(amount: integer, route: string[])integerPerforms amount exchange.

Examples

Starting with exchanges BRONZE -> SILVER at 3 and SILVER -> GOLD at 4:

StepOperationResult
1hasLane("BRONZE", "SILVER")true
2hasLane("SILVER", "BRONZE")false
3routeMultiplier(["BRONZE", "SILVER", "GOLD"])12
4exchange(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.

PRIVATE WORKSPACE

Checking your session…

The statement is public. The editor, editorial, submissions, and saved work are private.