FastPrepUnit Conversion Across Independent Families

Unit Conversion Across Independent Families

Apple logoApple● MediumFULLTIMEPHONE SCREEN
Learn

Problem statement

Each row in conversions is [source, target, rate] and means one unit of source equals rate units of target. A relationship may also be traversed backward using its reciprocal. Conversion families may be disconnected.

For every [from, to] query, return the product of rates along a conversion path, formatted with exactly six decimal places. Return UNKNOWN when either name does not appear or no path connects the two units. A known unit converted to itself is 1.000000.

Function

queryUnitConversions(conversions: String[][], queries: String[][]) → String[]

Examples

Example 1

conversions = [["C","F","1.8"],["m","cm","100"]]queries = [["C","F"],["F","C"],["C","cm"],["m","cm"]]return = ["1.800000","0.555556","UNKNOWN","100.000000"]

Temperature and length queries work within their own families, while a cross-family query is unknown.

Example 2

conversions = [["a","b","2"],["b","c","3"]]queries = [["a","c"],["c","a"],["b","b"]]return = ["6.000000","0.166667","1.000000"]

Path products, reciprocal traversal, and identity queries share one component.

Constraints

  • 0 <= conversions.length, queries.length <= 100000.
  • Rates are positive decimal strings.
  • All relationships are mutually consistent, so every path between the same pair has the same product.
  • Unit names are nonempty and contain no spaces.

More Apple problems

See Apple hiring insights
public String[] queryUnitConversions(String[][] conversions, String[][] queries) {
  // write your code here
}
conversions[["C","F","1.8"],["m","cm","100"]]
queries[["C","F"],["F","C"],["C","cm"],["m","cm"]]
expected["1.800000", "0.555556", "UNKNOWN", "100.000000"]
Checking account…