Skip to main content

Traffic System Validator

Traffic lights look simple.

Red.
Yellow.
Green.

But behind that simplicity is strict order.

If green follows green - it’s wrong.
If red jumps directly to green - dangerous.
If all signals are green - catastrophic.

This project is about validating order.

Not drawing lights.
Not building UI.

Enforcing rules.

What You Are Building

You will create a program that:

  • Accepts a sequence of traffic light states
  • Validates whether the transitions follow legal traffic rules
  • Detects invalid or unsafe sequences

We are not simulating cars.

We are validating system correctness.

Watch First

Why This Project Matters

This project trains:

  • State transitions
  • Sequential validation
  • Rule enforcement
  • Defensive design
  • Structured flow control

It introduces an important concept:

Systems evolve through states.

And states must follow strict transitions.

Define the Rules Clearly

Before coding, define allowed transitions.

Standard traffic light sequence:

Green → Yellow
Yellow → Red
Red → Green

No other transitions allowed.

Invalid examples:

  • Green → Red
  • Yellow → Green
  • Red → Yellow

Also invalid:

  • Same state repeated continuously (optional rule)
  • Unknown state like "Blue"
tip

If transitions are unclear, your validation will fail. Define the allowed paths before coding.

Step One - Naive Validation

Let’s validate a single transition first.

def is_valid_transition(current, next_state):
if current == "Green" and next_state == "Yellow":
return True
if current == "Yellow" and next_state == "Red":
return True
if current == "Red" and next_state == "Green":
return True
return False

Simple.

Direct.

Readable.

Validate a Full Sequence

Now we validate an entire list.

def validate_sequence(sequence):
for i in range(len(sequence) - 1):
if not is_valid_transition(sequence[i], sequence[i + 1]):
return False
return True

Test it:

sequence = ["Green", "Yellow", "Red", "Green"]
print(validate_sequence(sequence))

This works.

But it assumes:

  • All states are valid
  • Input is clean
  • Case is correct

That is fragile.

Improve the Structure

Let’s make it safer.

ALLOWED_STATES = {"Green", "Yellow", "Red"}

def is_valid_transition(current, next_state):
transitions = {
"Green": "Yellow",
"Yellow": "Red",
"Red": "Green"
}

return transitions.get(current) == next_state


def validate_sequence(sequence):
for state in sequence:
if state not in ALLOWED_STATES:
return False

for i in range(len(sequence) - 1):
if not is_valid_transition(sequence[i], sequence[i + 1]):
return False

return True

Now we:

  • Centralized rules
  • Reduced repeated conditionals
  • Increased clarity

This is better design.

Think Deeper - What Is This Really?

This is a finite state machine.

A system that:

  • Exists in one state at a time
  • Transitions based on rules
  • Rejects invalid transitions

Even simple systems use this concept:

  • Login flows
  • Order processing
  • Payment authorization
  • Network protocols

You just built a tiny state machine.

Edge Case Thinking

Ask yourself:

  • What if sequence is empty?
  • What if sequence has one element?
  • Should same state repeated be allowed?
  • Should case-insensitive matching be handled?
  • Should system detect loops?

Edge cases define robustness.

Extend It - Add Pedestrian Signal

Now imagine adding:

Walk → Don't Walk

These signals depend on main traffic signal.

Now validation becomes dependent.

State relationships grow.

Structure becomes more important.

Growth Reflection

Current complexity:

  • O(n) - linear through sequence

Efficient.

Now imagine validating:

  • 1 million state logs
  • Multiple intersections
  • Real-time traffic data

Now system design matters.

But logic foundation stays the same.

Interview Extension

Enhance system to:

  • Return index where violation occurs
  • Suggest expected next state
  • Log invalid transitions
  • Simulate automatic correction
  • Detect if light gets stuck

Now the validator becomes diagnostic.

Engineering Reflection

Traffic systems cannot afford ambiguity.

Rule clarity prevents accidents.

In code:

Rule clarity prevents bugs.

This project trains:

  • Structured thinking
  • Transition discipline
  • Predictive reasoning
  • Defensive validation

These skills scale.

Final Thought

A traffic light is simple.

But enforcing order in evolving systems is not.

If you can model transitions cleanly,

You are learning how to design safe systems.

And safe systems are the foundation of serious engineering.

© 2026 EngineersOfAI. All rights reserved.