Watch 10 video solutions for Convert the Temperature, a easy level problem involving Math. This walkthrough by Developer Docs has 1,113 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
You are given a non-negative floating point number rounded to two decimal places celsius, that denotes the temperature in Celsius.
You should convert Celsius into Kelvin and Fahrenheit and return it as an array ans = [kelvin, fahrenheit].
Return the array ans. Answers within 10-5 of the actual answer will be accepted.
Note that:
Kelvin = Celsius + 273.15Fahrenheit = Celsius * 1.80 + 32.00
Example 1:
Input: celsius = 36.50 Output: [309.65000,97.70000] Explanation: Temperature at 36.50 Celsius converted in Kelvin is 309.65 and converted in Fahrenheit is 97.70.
Example 2:
Input: celsius = 122.11 Output: [395.26000,251.79800] Explanation: Temperature at 122.11 Celsius converted in Kelvin is 395.26 and converted in Fahrenheit is 251.798.
Constraints:
0 <= celsius <= 1000Problem Overview: You are given a temperature value in Celsius. The task is to convert it into Kelvin and Fahrenheit using the standard mathematical formulas and return both results. The output is typically returned as a two-element array where the first value is Kelvin and the second is Fahrenheit.
Approach 1: Direct Calculation Approach (Time: O(1), Space: O(1))
The most straightforward solution applies the known temperature conversion formulas directly. Kelvin is computed as celsius + 273.15, and Fahrenheit is computed as celsius * 1.80 + 32.00. Since the problem only requires evaluating two arithmetic expressions, the algorithm performs a constant number of operations regardless of input size.
This approach uses basic arithmetic from math. You read the input value, calculate the two conversions, and store them in a result array or list. No loops, recursion, or extra data structures are required. The time complexity is O(1) because the number of operations never grows, and the space complexity is also O(1) since only two output values are stored.
This is the cleanest and most common solution used in practice. It mirrors how real systems implement temperature conversion utilities. Precision is handled automatically by floating‑point arithmetic in most programming languages.
Approach 2: Separate Functions for Each Conversion (Time: O(1), Space: O(1))
Another design separates the conversion logic into dedicated helper functions. One function converts Celsius to Kelvin, and another converts Celsius to Fahrenheit. Each function performs a single formula calculation and returns the result.
The main function calls both helpers and combines the results into an output array. This structure slightly increases code organization and readability, especially if conversions are reused elsewhere in a larger system. The computational work remains constant because each helper performs a single arithmetic operation.
This pattern reflects good modular design often seen in larger codebases that work with unit conversions or scientific utilities. The algorithmic complexity is still O(1) time and O(1) space because the number of computations and stored values remains fixed. The problem itself primarily tests understanding of mathematical formulas and basic implementation.
Recommended for interviews: The direct calculation approach is what interviewers expect. The problem focuses on correctly applying formulas and returning the results in the required format. Implementing it in a few lines shows clarity and correctness. The separate‑function version demonstrates clean code organization, but algorithmically both approaches are identical and run in constant time.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Direct Calculation Approach | O(1) | O(1) | Best choice for interviews and simple implementations where only one conversion operation is required. |
| Separate Functions for Each Conversion | O(1) | O(1) | Useful in larger systems where temperature conversions are reused across multiple modules. |