Talentd
FleetCode
Make and follow sheets.Sign in

Traffic Signal Color solution and explanation

EasyMathSimulationString3 min read82.7% acceptanceUpdated 27 Jul 2026
Traffic Signal Color | Leetcode 3894 | Explanation With Code | Javacodewithsitaram · 3:07 · 173 viewsWatch on YouTube
1 more video on this problem
  1. Traffic Signal Color | Leetcode 3894 | Explanation With Code | Javacodewithsitaram · 3:07 · 173 views
  2. Traffic Signal Color | #leetcode #biweeklycontest180 | #3894UMANG AGRAWAL · 1:12 · 4 views

The problem

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

class Solution:
    def trafficSignal(self, timer: int) -> str:
        if timer == 0:
            return "Green"
        if timer == 30:
            return "Orange"
        if 30 < timer <= 90:
            return "Red"
        return "Invalid"
class Solution {
    public String trafficSignal(int timer) {
        if (timer == 0) {
            return "Green";
        }
        if (timer == 30) {
            return "Orange";
        }
        if (timer > 30 && timer <= 90) {
            return "Red";
        }
        return "Invalid";
    }
}
class Solution {
public:
    string trafficSignal(int timer) {
        if (timer == 0) {
            return "Green";
        }
        if (timer == 30) {
            return "Orange";
        }
        if (timer > 30 && timer <= 90) {
            return "Red";
        }
        return "Invalid";
    }
};
function trafficSignal(timer: number): string {
    if (timer === 0) {
        return 'Green';
    }
    if (timer === 30) {
        return 'Orange';
    }
    if (timer > 30 && timer <= 90) {
        return 'Red';
    }
    return 'Invalid';
}
func trafficSignal(timer int) string {
	switch {
	case timer == 0:
		return "Green"
	case timer == 30:
		return "Orange"
	case timer > 30 && timer <= 90:
		return "Red"
	default:
		return "Invalid"
	}
}
Try this approach in the editor

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

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.

Comments

Now write it yourselfClose this page and solve Traffic Signal Color in the editor, without looking.
Practise Traffic Signal Color