Design Patterns: Structural · Coding problem

Good Night, Good Morning: A Smart-Home Facade

A free coding problem from the Facade Pattern skill. Read it here, then solve it in the app in Java or Python.

  • Easy
  • Coding problem
  • Free
  • Java and Python

The problem

Four smart-home devices are already written: Lights, Thermostat, DoorLocks and Alarm. Nobody wants to operate them one by one every evening. Implement three one-tap routines in SmartHomeFacade, calling the devices in exactly this order:

  • goodNight() - lights off in every room (ROOMS, in order), thermostat to 18, lock all doors, arm the alarm
  • goodMorning() - disarm the alarm, unlock the front door, thermostat to 21, kitchen lights on
  • leaveHome() - lights off in every room, thermostat to 16, lock all doors, arm the alarm

The alarm refuses to arm while a door is unlocked - which is exactly the kind of rule a facade should get right once, so no caller has to remember it.

In Python: the routines are good_night(), good_morning() and leave_home(); the devices' methods are lock_all(), unlock_front(), arm(doors) and disarm(), and the alarm raises RuntimeError while a door is unlocked.

Commands: GOODNIGHT, GOODMORNING, LEAVE, STATUS. Output: the device actions triggered by each command, joined with ; (or the status line).

Example

Input:
2
GOODNIGHT
STATUS

Output:
lights off: living; lights off: kitchen; lights off: bedroom; thermostat 18; doors locked; alarm armed
alarm=armed doors=locked

Constraints

  • At most 100 commands.

How it's graded

  • Run checks your code against the examples. Submit also runs hidden tests, and you see pass or fail for them, never their inputs.
  • Stuck? Hints come in four steps, from a nudge to a code skeleton, each costing part of the points.
  • Once you pass, you see the approaches from brute force to optimal, and where your solution lands among them.

More problems in Facade Pattern

Solve it Good Night, Good Morning: A Smart-Home Facade free

No card needed. Lessons, practice questions and easy problems are free forever.

Free plan, no card · 7-day Pro trial · Cancel anytime