FastPrepUnit Conversion Across Independent Families
Problems/Unit Conversion Across Independent Families

Unit Conversion Across Independent Families

Apple logoApple● MediumFULLTIMEPHONE SCREEN
QuestionFunctionExamplesConstraints

Problem statement

Source note: The interview report establishes the task identity and central capability. This practice contract supplies only the portable interface, deterministic boundary rules, and output serialization needed for consistent execution.

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

  • Evaluate DivisionPHONE SCREEN · Seen Nov 2026
  • Reverse a Linked ListPHONE SCREEN · Seen Nov 2026
  • Validate a BST with a Custom ComparisonPHONE SCREEN · Seen Nov 2026
All Apple interview questionsSee Apple hiring insights
Saved
class Solution {
    public String[] queryUnitConversions(String[][] conversions, String[][] queries) {
      // write your code here
    }
}
Checking account…