AlgoMaster Logo
AlgoMasterDesign Traffic Light Controllereasy

Design Traffic Light Controller

easy

Design the Color model used by a TrafficLight controller. The controller displays one colour at a time and moves through the same repeating cycle: red, green, yellow, then back to red.

The TrafficLight class is provided. Complete the missing Color enum—or the closest enum-like type in your language—so the controller has everything it needs to behave as follows:

  • TrafficLight(String startColor) starts on RED, GREEN, or YELLOW, matching the input without regard to case. An unrecognized value defaults to RED.
  • String getColor() returns the current colour's name.
  • int getDuration() returns the current colour's duration: RED lasts 30 seconds, GREEN lasts 25, and YELLOW lasts 5.
  • String next() advances once through the cycle and returns the new colour's name.
  • String describe() returns the current colour and duration in the exact format "<COLOR> (<duration>s)", such as "RED (30s)".

Each colour should know both how long it lasts and which colour follows it:

RED (30s) -> GREEN (25s) -> YELLOW (5s) -> RED (30s)

Read the supplied controller carefully to see the exact fields, methods, or lookup tables expected from Color in your language. Do not duplicate the cycle rules inside the controller.

Example 1:

Input:

Output:

Explanation: The light starts red for 30 seconds. Advancing moves it to green, which shows for 25.

Example 2:

Input:

Output:

Explanation: Red to green to yellow and back to red completes one full cycle.

Constraints:
  • startColor consists of English letters.
  • At most 100 calls will be made across all methods.

How the design is graded

needs 7/10 to pass
  • Enum modelling

    Full marks when the three colours are a fixed set (an enum, or the closest equivalent the language offers) and each colour's duration is owned by that set, whether as a field on the enum member or as a single table or method keyed by the colour. Lose points for parallel string and integer arrays, for loose colour strings with the durations kept elsewhere, or for getDuration on the controller working the value out itself instead of asking the colour for it.

  • Cycle containment

    Full marks when the successor of each colour is defined once alongside the colour set, so TrafficLight.next only moves to whatever the current colour reports as its successor and returns it. Lose points when the cycle order is spelled out inside TrafficLight.next itself, or when the same ordering is written out in more than one place.

  • Structure and naming

    Full marks for handling an unrecognised starting colour deliberately, descriptive names, and no dead code. Lose points for printing to stdout or for the durations appearing as bare numbers in more than one place.

Passing every test is not enough on its own. A submission is accepted only when the design also clears the bar.

Hints

Loading...
CallReturns
new TrafficLight("RED")null
getColor()"RED"
getDuration()30
next()"GREEN"
getColor()"GREEN"
getDuration()25

The light starts on RED for 30 seconds. Advancing moves it to GREEN, which lasts 25.

Run checks these cases. Submit also runs a larger hidden set.