Next Traffic Light

Given the current state of a traffic light, represented by a TrafficLight enumeration, containing three possible states: Red, Yellow, and Green.

The traffic light follows a fixed, repeating sequence:

Red → Green → Yellow → Red

The Task is to determine and return the next traffic light state in this sequence. After Yellow, the sequence repeats from Red.

Note:The input currentis represented by an integer, where 0 = Red, 1 = Yellow, and 2 = Green. The driver converts this integer into the corresponding TrafficLightenumeration value before calling nextLight().

Example 1:

Input: current = 0

Output: "Green"

Explanation: The integer 0 represents TrafficLight::Red. Green follows Red in the specified cycle, so the function returns TrafficLight::Green. The driver prints its name, Green.

Example 2:

Input: current = 2

Output: "Yellow"

Explanation: The integer 2 represents TrafficLight::Green. The next state after Green is Yellow, so the function returns TrafficLight::Yellow, which the driver prints as Yellow.

Now Your Turn!

Pick the correct output for the given input

Input: current = 1

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • 0 <= current <= 2

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

0
/**
* Enumeration supplied by the platform:
* enum class TrafficLight {
* Red,
* Yellow,
* Green
* };
*/
class Solution {
public:
TrafficLight nextLight(TrafficLight current) {
// Write your code here
}
};
 
Test Case

Input:

Current