Skip to main content

Traffic Signal Color - Video Solutions

EasyMathStringSimulation

Traffic Signal Color | Leetcode 3894 | Explanation With Code | Java

codewithsitaram
3:07173 views
2 video solutions available

Traffic Signal Color - Video Solution

Watch 2 video solutions for Traffic Signal Color, a easy level problem involving Math, String, Simulation. This walkthrough by codewithsitaram has 173 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.

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
Read full problem with examples

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.

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