FastPrepShortest Currency Conversion Chain

Shortest Currency Conversion Chain

Bloomberg LP logoBloomberg LP● MediumNEW GRADONSITE INTERVIEW
Learn

Problem statement

Each row in pairs lists two currencies with a direct conversion and may be traversed in either direction. Return a chain from source to target using the fewest edges.

Break equal-hop ties by lexicographically comparing the complete currency sequences. Return an empty array when disconnected.

Function

shortestConversionChain(pairs: String[][], source: String, target: String) → String[]

Examples

Example 1

pairs = [["USD","CAD"],["CAD","MEX"],["USD","EUR"],["EUR","MEX"]]source = "USD"target = "MEX"return = ["USD","CAD","MEX"]

Both routes use two edges; the CAD route is lexicographically smaller.

Constraints

  • Currency names are nonempty strings.
  • Pairs contain no self edges.

More Bloomberg LP problems

See Bloomberg LP hiring insights
public String[] shortestConversionChain(String[][] pairs, String source, String target) {
  // Write your code here.
}
pairs[["USD","CAD"],["CAD","MEX"],["USD","EUR"],["EUR","MEX"]]
source"USD"
target"MEX"
expected["USD", "CAD", "MEX"]
Checking account…