We use cookies (including Google cookies) to personalize ads and analyze traffic. By continuing to use our site, you accept our Privacy Policy.

Convert the Temperature

Number: 2556

Difficulty: Easy

Paid? No

Companies: Google, Amazon


Problem Description

Given a non-negative floating point number representing the temperature in Celsius, convert it into Kelvin and Fahrenheit. Use the formulas Kelvin = Celsius + 273.15 and Fahrenheit = Celsius * 1.80 + 32.00, and return the results as an array [kelvin, fahrenheit].


Key Insights

  • The conversion formulas are straightforward arithmetic operations.
  • Kelvin is obtained by simply adding 273.15 to the Celsius temperature.
  • Fahrenheit is calculated by multiplying the Celsius value by 1.80 and then adding 32.
  • The solution runs in constant time and space since it requires only basic arithmetic operations without iterations.

Space and Time Complexity

Time Complexity: O(1)
Space Complexity: O(1)


Solution

The approach is to directly apply the arithmetic formulas to convert Celsius to Kelvin and Fahrenheit. No complex data structures or algorithms are required. We simply compute:

  1. Kelvin = Celsius + 273.15
  2. Fahrenheit = Celsius * 1.80 + 32.00 These computed values are then returned as an array. This method is efficient and trivial with constant time and space requirements.

Code Solutions

# Function to convert Celsius temperature to Kelvin and Fahrenheit
def convert_temperature(celsius):
    # Calculate Kelvin by adding 273.15 to the Celsius value
    kelvin = celsius + 273.15
    # Calculate Fahrenheit by applying the formula: Celsius * 1.80 + 32.00
    fahrenheit = celsius * 1.80 + 32.00
    # Return the result as a list [kelvin, fahrenheit]
    return [kelvin, fahrenheit]

# Example usage:
if __name__ == "__main__":
    # Test with a sample input: 36.50 Celsius
    result = convert_temperature(36.50)
    print(result)  # Expected output: [309.65, 97.70]
← Back to All Questions