FastPrepMinimum Currency Conversions

Minimum Currency Conversions

EPAM logoEPAM● MediumFULLTIMEPHONE SCREEN
Learn

Problem statement

Each row in pairs contains two currency codes that can be converted directly in either direction.

Return the minimum number of direct conversions needed to move from source to target. Return -1 when no conversion chain exists. If the two codes are equal, return 0.

Function

minimumConversions(pairs: String[][], source: String, target: String) → int

Examples

Example 1

pairs = [["USD","EUR"],["EUR","JPY"],["USD","GBP"]]source = "USD"target = "JPY"return = 2

The shortest chain is USD to EUR to JPY.

Example 2

pairs = [["A","B"],["C","D"]]source = "A"target = "D"return = -1

The currencies are in disconnected components.

Constraints

  • 0 <= pairs.length <= 10000.
  • Every row contains two nonempty currency codes.
  • Codes contain uppercase English letters.

Source note: Author-written prompt excerpt retained for source fidelity.

More EPAM problems

See EPAM hiring insights
public int minimumConversions(String[][] pairs, String source, String target) {
    // Write your code here.
}
pairs[["USD","EUR"],["EUR","JPY"],["USD","GBP"]]
source"USD"
target"JPY"
expected2
Checking account…