4
\$\begingroup\$

I had a phone interview and we shared a screen to do a simple challenge. Instructions were ambiguous but I basically had to implement how an air conditioner would work in general. I wrote the following as I talked it out with the interviewer.

So I made a class with a parameter that would be a room temp and then added two properties, heatingSystem and coolingSystem and set them as booleans. Then wrote a function called setTemperature that should’ve been a while loop that changes the temperature and setting of the A/C until it reaches the set temperature.

class AirConditioner {
   constructor(temperature) {
       this.temperature = temperature;
       this.coolingSystem = false;
       this.heatingSystem = false;
   }



  temperatureSet(temperature) {
    while (this.temperature != temperature) {
       if (this.temperature < temperature) {
           // turn on the cooling system and turn off heating system
           if (this.coolingSystem) {
               this.coolingSystem = false;
               this.heatingSystem = true;
               this.temperature++;
           } else {
             this.heatingSystem = true;
             console.log('Temperature has been lowered to ' + this.temperature);
             this.temperature++;
           }
       } else if (this.temperature > temperature) {
           // turn off the cooling system and turning the heating
           if (this.heatingSystem) {
               this.coolingSystem = true;
               this.heatingSystem = false;
               console.log('Temperature has been increased to ' + this.temperature);
               this.temperature--;
           } else {
             this.coolSystem = true;
             console.log('Temperature has been lowered to ' + this.temperature);
             this.temperature--;
           }
       } else {
           // turn off cooling/heating system but keep the fan un
           console.log('temp is the same')
       }
    }
      console.log(`this.temperature: ${this.temperature} temperature: ${temperature}`)
  }
}

const AC = new AirConditioner(78);
console.log(AC);
AC.temperatureSet(63);
console.log(AC);

I honestly don't like the code and can already see a lot of possibilities on how to refactor or add other functions for re-usability. I was wondering what your suggestions are.

\$\endgroup\$
  • \$\begingroup\$ Was it a requirement for the interview that you use OOP? \$\endgroup\$ – Aaron Goldsmith Apr 17 '18 at 4:19
1
\$\begingroup\$

I don't know the position you were interviewed for. Below is the embedded engineer point of view.

  • A room temp is not a class parameter. The AC cannot control it directly, so this.temperature++ and this.temperature-- are incorrect. The AC may only read the temp sensor.

  • The desired temperature OTOH is a class parameter. You should go an extra mile and provide the interface to set it. BTW, I'd expect temperatureSet to do just that: set the desired temperature.

  • As implemented, the program terminates as soon as a certain temperature is reached. I expect an AC system to run more or less forever.

  • In the if (this.temperature < temperature) clause you turn on heater no matter what. Move the this.heatingSystem = true; line outside of this.coolingSystem clause.

Assuming the above recommendations are implemented, the basic functionality should look like this:

    while(true) {
        ambientTemperature = this.temperatureSensor.read();
        if (ambientTemperature < this.targetTemperature) {
            if (this.coolingSystem) {
                this.coolingSystem = false;
            } else {
                console.log('Temperature has been lowered to ' + ambientTemperature);
            }
            this.heatingSystem = true;
        } else {
        ....

Of course there is a plenty of room to improve, but again that depends on interviewer expectations, and the AC schematics you'd agree upon.

\$\endgroup\$
  • \$\begingroup\$ I really like the way you did this answer, tying it directly to the HVAC. \$\endgroup\$ – Raystafarian Apr 13 '18 at 4:46
  • \$\begingroup\$ Given the nature of having to switch off the cooling boolean when the heating boolean is enabled, it might be better to simply use an int with 3 states so you don't have to set two values at the same time (and risk going out of sync when you make a mistake). Or, alternatively, calculate needsHeating and needsCooling on the fly (by comparing the temperatures) \$\endgroup\$ – Flater Apr 13 '18 at 11:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.