You are given an integer timer representing the remaining time (in seconds) on a traffic signal.
The signal follows these rules:
timer == 0, the signal is "Green"timer == 30, the signal is "Orange"30 < timer <= 90, the signal is "Red"Return the current state of the signal. If none of the above conditions are met, return "Invalid".
Example 1:
Input: timer = 60
Output: "Red"
Explanation:
Since timer = 60, and 30 < timer <= 90, the answer is "Red".
Example 2:
Input: timer = 5
Output: "Invalid"
Explanation:
Since timer = 5, it does not satisfy any of the given conditions, the answer is "Invalid".
Constraints:
0 <= timer <= 1000Problem Overview: You are given the color of a traffic signal and must return the correct driving action. The mapping is straightforward: red → stop, yellow → slow down, and green → go. The task tests basic control flow and conditional logic.
Approach 1: Conditional Checks (If-Else) (Time: O(1), Space: O(1))
The simplest solution uses sequential conditional checks. Compare the input color against the three valid values and return the corresponding action. For example, if the color equals "red", return "stop"; if it equals "yellow", return "slow down"; otherwise return "go". Since the number of possible colors is fixed, the program performs a constant number of comparisons. This approach relies on basic conditional logic and is typically the first implementation developers write during interviews or quick coding tasks.
The key insight is that the decision space is extremely small. There is no need for loops, sorting, or advanced structures. Each condition directly maps a specific string to its corresponding action. Time complexity stays O(1) because the number of comparisons never grows with input size, and memory usage remains O(1) since no additional storage is required.
Approach 2: Hash Map / Lookup Table (Time: O(1), Space: O(1))
Another clean approach stores the mapping in a lookup table such as a dictionary or hash map: {"red": "stop", "yellow": "slow down", "green": "go"}. You then perform a direct lookup using the input color as the key. Hash table access is constant time on average, giving the same O(1) time complexity. The space complexity is also O(1) because the mapping contains only three fixed entries.
This approach is often preferred when mappings grow larger or when you want to separate configuration from logic. Instead of multiple conditional branches, the behavior is centralized in a single structure. This technique appears frequently in problems involving hash tables or simple simulation rules.
Recommended for interviews: The conditional if-else approach is usually expected because it shows you clearly understand control flow. The lookup table version demonstrates slightly cleaner design and scalability. Either solution runs in constant time and space, but explaining both approaches shows stronger reasoning and familiarity with multiple implementation styles.
Solutions for this problem are being prepared.
Try solving it yourself| Approach | Time | Space | When to Use |
|---|---|---|---|
| If-Else Conditional Checks | O(1) | O(1) | Best for very small decision sets with only a few known cases |
| Hash Map / Lookup Table | O(1) | O(1) | Cleaner design when mappings grow larger or need easy modification |
Practice Traffic Signal Color with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor