Skip to main content

Traffic Signal Color - Solution & Explanation

EasyMathStringSimulation4 min readAsked at: Google
Practice this problem

Problem Statement

You are given an integer timer representing the remaining time (in seconds) on a traffic signal.

The signal follows these rules:

  • If timer == 0, the signal is "Green"
  • If timer == 30, the signal is "Orange"
  • If 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 <= 1000

Approach Overview

Problem 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.

Solution

We determine the answer according to the conditions described in the problem and return the corresponding string.

The time complexity is O(1), and the space complexity is O(1).

Code

Python

Java

C++

Go

TypeScript

Try this approach in the editor →

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
If-Else Conditional ChecksO(1)O(1)Best for very small decision sets with only a few known cases
Hash Map / Lookup TableO(1)O(1)Cleaner design when mappings grow larger or need easy modification

Video Solution

Traffic Signal Color | Leetcode 3894 | Explanation With Code | Java • codewithsitaram • 173 views views

Watch 1 more video solutions →

Frequently Asked Questions

Is Traffic Signal Color easy or hard?
Traffic Signal Color is classified as an Easy problem. It mainly tests understanding of basic conditionals, string comparison, and simple control flow rather than advanced algorithms or data structures.
Traffic Signal Color Python/Java solution
In Python, you can implement the solution using an if-elif chain or a dictionary mapping colors to actions. In Java, use if-else statements or a HashMap with string keys. Both implementations run in O(1) time and require O(1) space.
How to solve Traffic Signal Color in O(1)?
Use direct conditional checks or a dictionary lookup. Compare the input string with "red", "yellow", and "green" and return the corresponding action. Because the number of checks is fixed, the solution runs in constant time and uses constant memory.
What is the best approach for Traffic Signal Color?
The best approach uses simple conditional checks or a lookup table mapping signal colors to actions. Both run in O(1) time because the number of possible inputs (red, yellow, green) is fixed. The if-else version is commonly expected in interviews, while a hash map provides a cleaner and more scalable structure.
Is Traffic Signal Color asked at Google/Amazon/Meta?
Problems like Traffic Signal Color appear in entry-level coding screens or practice sets rather than advanced interview rounds. Large companies such as Google, Amazon, and Meta often include similar conditional logic or simulation questions to test basic programming fundamentals.
What data structure is used in Traffic Signal Color?
The simplest implementation uses conditional statements without any data structure. An alternative uses a hash map or dictionary to map each color string to its corresponding driving action, enabling constant-time lookups.
What is the time complexity of Traffic Signal Color?
The time complexity is O(1). The algorithm performs a constant number of comparisons or a single hash lookup regardless of input size. Since the problem only handles three possible signal colors, execution time never increases.

Ready to solve this problem?

Practice Traffic Signal Color with our built-in code editor and test cases.

Practice on FleetCode