Skip to main content

Convert the Temperature - Solution & Explanation

EasyMath11 min readAsked at: Amazon, Google
Practice this problem

Problem Statement

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.15
  • Fahrenheit = 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 <= 1000

Approach Overview

Problem 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 1: Direct Calculation Approach

This approach involves directly using the given formulas to convert the Celsius temperature to Kelvin and Fahrenheit. We simply apply the mathematical operations provided and return the results as an array.

In this C solution, we define a function convertTemperature that accepts a Celsius value and a pointer to a result array. The function calculates the Kelvin and Fahrenheit values and stores them in the result array. The main function demonstrates using this function with a sample Celsius value.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(1) - Constant operations.
Space Complexity: O(1) - Only a fixed amount of space is used.

Try this approach in the editor →

Approach 2: Separate Functions for Each Conversion

In this approach, we define separate functions for converting Celsius to Kelvin and Celsius to Fahrenheit. Each function handles just one conversion, and a main function invokes both to fetch the final results.

In this C solution, we define two separate functions: toKelvin and toFahrenheit. They handle individual conversions. A convertTemperature function is used to gather results into the output array, and main function is used for demonstration.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(1) - Basic operations in separate functions.
Space Complexity: O(1) - A constant amount of space is used regardless of input.

Try this approach in the editor →

Approach 3: Simulation

We can directly simulate according to the problem description.

The time complexity is O(1), and the space complexity is O(1).

Code

Python

Java

C++

Go

TypeScript

Rust

C

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
Direct Calculation Approach

Time Complexity: O(1) - Constant operations.
Space Complexity: O(1) - Only a fixed amount of space is used.

Separate Functions for Each Conversion

Time Complexity: O(1) - Basic operations in separate functions.
Space Complexity: O(1) - A constant amount of space is used regardless of input.

Simulation—

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Direct Calculation ApproachO(1)O(1)Best choice for interviews and simple implementations where only one conversion operation is required.
Separate Functions for Each ConversionO(1)O(1)Useful in larger systems where temperature conversions are reused across multiple modules.

Video Solution

Leetcode | 2469. Convert the Temperature | Easy | Java Solution • Developer Docs • 1,240 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Convert the Temperature easy or hard?
Convert the Temperature is an Easy difficulty problem with a very high acceptance rate. It mainly checks whether you know the correct conversion formulas and can return results in the required format.
Convert the Temperature Python/Java solution
The Python or Java implementation directly applies the formulas and returns the two values in a list or array. Example logic: kelvin = celsius + 273.15 and fahrenheit = celsius * 1.80 + 32.00, then return [kelvin, fahrenheit]. The complexity remains O(1).
How to solve Convert the Temperature in O(n)?
The problem does not require O(n) processing because there is only a single numeric input. The optimal implementation runs in O(1) time by applying two direct formulas: Kelvin = celsius + 273.15 and Fahrenheit = celsius * 1.80 + 32.00.
What is the best approach for Convert the Temperature?
The direct calculation approach is the best solution. Compute Kelvin using celsius + 273.15 and Fahrenheit using celsius * 1.80 + 32.00, then return both values in an array. The algorithm runs in O(1) time and O(1) space because it performs only a few arithmetic operations.
Is Convert the Temperature asked at Google/Amazon/Meta?
Convert the Temperature is categorized as an easy math problem on LeetCode. It is less common in real interviews at companies like Google or Meta but is useful for practicing problem comprehension, precision with floating-point calculations, and simple implementation tasks.
What data structure is used in Convert the Temperature?
No complex data structure is required. The solution simply returns a small array or list containing two floating-point values: the Kelvin and Fahrenheit conversions. The focus is on applying mathematical formulas correctly.
What is the time complexity of Convert the Temperature?
The time complexity is O(1). The solution performs two constant-time arithmetic calculations and stores the results in a fixed-size array. No loops, recursion, or input-dependent operations are involved.

Ready to solve this problem?

Practice Convert the Temperature with our built-in code editor and test cases.

Practice on FleetCode