Taro Logo

Convert the Temperature

Easy
Asked by:
Profile picture
Profile picture
Profile picture
Profile picture
11 views

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

Solution


Clarifying Questions

When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:

  1. What temperature scale is the input given in and what temperature scale should I return?
  2. What are the acceptable temperature ranges for the input?
  3. What precision should I use for the returned converted temperature (e.g., decimal places)?
  4. If the input is invalid, how should I handle the error (e.g., throw an exception, return null, or return a specific error code)?
  5. Are there any specific conversion formulas I should be using, or am I free to use standard conversions?

Brute Force Solution

Approach

The brute force strategy for temperature conversion involves directly applying the conversion formulas. We'll calculate both Celsius and Kelvin temperatures from the given Fahrenheit temperature using their respective formulas. Finally, we'll return both converted temperatures.

Here's how the algorithm would work step-by-step:

  1. Take the given Fahrenheit temperature.
  2. Calculate the Celsius temperature by applying the Fahrenheit-to-Celsius conversion formula.
  3. Calculate the Kelvin temperature by applying the Fahrenheit-to-Kelvin conversion formula.
  4. Return both the calculated Celsius and Kelvin temperatures as a result.

Code Implementation

def convert_the_temperature(fahrenheit_temperature):
    # Apply the Fahrenheit to Celsius conversion formula
    celsius_temperature = (fahrenheit_temperature - 32) * 5 / 9

    # Apply the Fahrenheit to Kelvin conversion formula

    kelvin_temperature = (fahrenheit_temperature - 32) * 5 / 9 + 273.15

    # Return Celsius and Kelvin temperatures
    return [celsius_temperature, kelvin_temperature]

Big(O) Analysis

Time Complexity
O(1)The described algorithm performs a fixed number of calculations: one Fahrenheit-to-Celsius conversion and one Fahrenheit-to-Kelvin conversion. The number of operations doesn't depend on the size of any input 'n'. Therefore, the time complexity is constant, or O(1).
Space Complexity
O(1)The algorithm calculates Celsius and Kelvin temperatures from a given Fahrenheit temperature. It uses a fixed number of variables to store the input Fahrenheit temperature and the calculated Celsius and Kelvin values. No additional data structures that scale with the input size N (where N is the Fahrenheit temperature) are used. Therefore, the auxiliary space used is constant, resulting in a space complexity of O(1).

Optimal Solution

Approach

The problem asks us to convert a temperature from Celsius to Kelvin and Fahrenheit. We can solve this by applying the defined conversion formulas directly. It's a straightforward application of basic arithmetic.

Here's how the algorithm would work step-by-step:

  1. Calculate the temperature in Kelvin by adding 273.15 to the Celsius temperature.
  2. Calculate the temperature in Fahrenheit by multiplying the Celsius temperature by 1.8 and then adding 32.
  3. Return both Kelvin and Fahrenheit temperatures as the output.

Code Implementation

def convert_temperature(celsius_temperature):
    # Calculate Kelvin. This is a direct conversion.
    kelvin_temperature = celsius_temperature + 273.15

    # Calculate Fahrenheit. This is a direct conversion.
    fahrenheit_temperature = (celsius_temperature * 1.8) + 32

    # Return both converted temperatures
    return [kelvin_temperature, fahrenheit_temperature]

Big(O) Analysis

Time Complexity
O(1)The solution involves a fixed number of arithmetic operations: one addition for Kelvin and one multiplication and one addition for Fahrenheit. The number of operations does not depend on the size of any input. Therefore, the time complexity is constant, or O(1).
Space Complexity
O(1)The algorithm calculates Kelvin and Fahrenheit temperatures using the provided formulas. It only stores the resulting Kelvin and Fahrenheit values in memory before returning them. These variables occupy a constant amount of space, irrespective of the input Celsius temperature (N). Therefore, the auxiliary space complexity is O(1).

Edge Cases

CaseHow to Handle
Input temperature is null or not a numberReturn an appropriate error message or throw an exception to indicate invalid input.
Input temperature is negative (below absolute zero)Check if the temperature is physically possible and handle it by returning an error or an empty result.
Input temperature is extremely large, potentially causing overflowUse appropriate data types (e.g., double) and check for potential overflow during calculations.
Input temperature is exactly 0 (Celsius or Fahrenheit)The conversion formulas should correctly handle zero input according to the defined standard.
Floating-point precision errors during conversionConsider rounding the results to a specified number of decimal places to avoid misleading very small differences.
The temperature scale is not valid or unrecognizedRaise error, use default value if acceptable, or attempt to resolve by other means (configuration, etc).
Very small non-zero input temperaturesEnsure the calculation does not lead to divide-by-zero errors or significant loss of precision.
Conversion to Kelvin yields zero or negative resultsIf converting to Kelvin ensure the answer is >=0, returning an error otherwise because this is physically impossible.