Design a Smart Thermostat
🌡️ What is a smart thermostat? It is the box on the wall that decides when to run the heating or the air conditioning. It reads temperature sensors around the house, knows what temperature you want at each time of day, and lets you override that by hand. Every few moments it makes one decision: heat, cool, or do nothing.
This easy problem looks like a few setters and getters, but it tests two ideas that come up again and again in LLD. First, one value (the target temperature) can come from several sources, and the design must say clearly which one wins. Second, the decision has memory: whether the furnace should run depends on whether it is already running. Get those two right and the rest is bookkeeping.
Requirements
"Design a smart thermostat. It has modes like heat and cool, follows a schedule, and reads temperature sensors."
Clarifying Questions
You: Which modes are there?
Interviewer: Off, heat, cool and auto. Heat only heats, cool only cools, auto does whichever is needed. There is one target temperature for all of them.
You: When exactly does the heat turn on and off? If it switches right at the target, the furnace would turn on and off every minute.
Interviewer: Good point. Use a deadband of 2 degrees. Heating starts when the room is 2 or more degrees below the target, and runs until the room reaches the target. Cooling is the mirror image.
You: What does a schedule look like?
Interviewer: A daily program: "from 7:00 the target is 22, from 22:00 it is 18". It repeats every day. Weekdays and weekends are the same for now.
You: What happens when someone turns the dial by hand?
Interviewer: That is a hold. It wins over the schedule until the next scheduled change, then the schedule takes over again.
You: How many sensors are there, and what if one stops reporting?
Interviewer: One per room, any number. Use the average of the sensors that reported in the last 10 minutes. If none did, the thermostat does not know the temperature and must not run anything.
You: How do we know the time, and when does the thermostat decide?
Interviewer: Pass the time in, in minutes. It decides again after every call.
The second answer is the heart of the problem. Without a deadband, a room sitting right at the target would switch the furnace on and off with every tiny change, which wears it out. With a deadband, the answer depends on what the thermostat was already doing, so the action becomes state we keep, not a value we compute fresh.
💡 Interview tip: When a device switches something on and off around a threshold, ask what happens right at the threshold. The answer is almost always "a band, not a line", and naming it (hysteresis) shows you have thought about real hardware.
Final Requirements
- The thermostat has a mode:
OFF,HEAT,COOLorAUTO, and an action:IDLE,HEATINGorCOOLING. - The room temperature is the average (rounded down) of the latest reading of every sensor that reported in the last 10 minutes. With no fresh reading, it is unknown.
- With target
Sand room temperatureT: heating starts atT <= S - 2and continues whileT < S; cooling starts atT >= S + 2and continues whileT > S.HEATmay only heat,COOLonly cool,AUTOboth. - In
OFFmode, or when the temperature is unknown, the action isIDLE. - A daily schedule maps start minutes to targets. The entry in force is the latest one at or before the current minute of the day, wrapping to the previous day's last entry.
- A manual target (a hold) wins over the schedule until the next scheduled change. With no schedule, it lasts until the next hold. With neither, a default target applies.
- The thermostat decides its action again after every call.
Out of scope
- Weekday schedules, vacations and learning your habits (see Extensibility).
- The fan, humidity, and talking to the actual furnace.
- The phone app, user accounts and energy reports.