Round 1
Questions: Given a file containing unit conversion rates, write a function that converts one unit to another. The file consists of JSON-like log entries where each entry specifies a conversion rate between two units.
Your function should support both direct conversions (e.g., meters to feet) and chained conversions (e.g., meters to inches through feet).
The function should load the conversion rates from the file, store them internally, and handle conversion requests dynamically. The units can be measurements like meters, feet, inches, centimeters, etc.
Sample Log File Entries:
{"from": "meters", "to": "feet", "rate": 3.281} {"from": "feet", "to": "inches", "rate": 12} {"from": "centimeters", "to": "inches", "rate": 0.3937} {"from": "meters", "to": "centimeters", "rate": 100}
Example Conversions:
- convert("meters", "inches", 2) → 78.744
- 2 meters → 6.562 feet (using meters to feet conversion rate of 3.281)
- 6.562 feet → 78.744 inches (using feet to inches conversion rate of 12)
- convert("centimeters", "inches", 100) → 39.37
- 100 centimeters → 39.37 inches (using centimeters to inches conversion rate of 0.3937)
- convert("meters", "centimeters", 1) → 100
- 1 meter → 100 centimeters (using meters to centimeters conversion rate of 100)
Code Implementation:
import java.io.*; import java.util.*; class UnitConversionRate { String fromUnit; String toUnit; double rate; UnitConversionRate(String fromUnit, String toUnit, double rate) { this.fromUnit = fromUnit; this.toUnit = toUnit; this.rate = rate; } } public class UnitConverter { private Map<String, Map<String, Double>> conversionRates = new HashMap<>(); // load the rates from the file public void loadRates(String fileName) throws IOException { BufferedReader reader = new BufferedReader(new FileReader(fileName)); String line; while ((line = reader.readLine()) != null) { UnitConversionRate rate = parseRate(line); addRate(rate); } reader.close(); } // parse the json-like string and return a UnitConversionRate object private UnitConversionRate parseRate(String line) { String[] parts = line.replaceAll("[{}\\"]", "").split(", "); String fromUnit = parts[0].split(" : ")[1]; String toUnit = parts[1].split(" : ")[1]; double rate = Double.parseDouble(parts[2].split(" : ")[1]); return new UnitConversionRate(fromUnit, toUnit, rate); } // store the conversion rate in a map private void addRate(UnitConversionRate rate) { conversionRates .computeIfAbsent(rate.fromUnit, k -> new HashMap<>()) .put(rate.toUnit, rate.rate); } // convert from one unit to another public double convert(String fromUnit, String toUnit, double amount) { if (conversionRates.containsKey(fromUnit) && conversionRates.get(fromUnit).containsKey(toUnit)) { return amount * conversionRates.get(fromUnit).get(toUnit); } // check for chained conversions for (String intermediaryUnit : conversionRates.get(fromUnit).keySet()) { if (conversionRates.containsKey(intermediaryUnit) && conversionRates.get(intermediaryUnit).containsKey(toUnit)) { double toIntermediary = conversionRates.get(fromUnit).get(intermediaryUnit); double toFinal = conversionRates.get(intermediaryUnit).get(toUnit); return amount * toIntermediary * toFinal; } } throw new IllegalArgumentException("Conversion rate not available for " + fromUnit + " to " + toUnit); } public static void main(String[] args) { UnitConverter converter = new UnitConverter(); try { converter.loadRates("unit_conversion_rates.log"); double result = converter.convert("meters", "inches", 2); System.out.println("Converted Amount: " + result); } catch (IOException e) { e.printStackTrace(); } } }
Candidate's Approach
No approach provided.
Interviewer's Feedback
No feedback provided.