Traffic light sequence in PLC Programming - Time & Space Complexity
We want to understand how the time it takes to run a traffic light sequence program changes as the program runs longer or handles more steps.
Specifically, we ask: how does the number of operations grow as the sequence repeats?
Analyze the time complexity of the following code snippet.
// Simple traffic light sequence
Timer GreenTimer = 10;
Timer YellowTimer = 3;
Timer RedTimer = 7;
while (true) {
TurnOn(GreenLight);
Wait(GreenTimer);
TurnOff(GreenLight);
TurnOn(YellowLight);
Wait(YellowTimer);
TurnOff(YellowLight);
TurnOn(RedLight);
Wait(RedTimer);
TurnOff(RedLight);
}
This code runs a traffic light sequence turning lights on and off with fixed wait times in a continuous loop.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The infinite
while (true)loop that cycles through turning lights on and off. - How many times: This loop repeats endlessly, running the sequence over and over.
Since the sequence runs forever, the number of operations grows steadily over time.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 cycles | About 10 times the steps in one sequence |
| 100 cycles | About 100 times the steps in one sequence |
| 1000 cycles | About 1000 times the steps in one sequence |
Pattern observation: The operations increase linearly with the number of cycles completed.
Time Complexity: O(n)
This means the total work grows directly in proportion to how many times the traffic light sequence runs.
[X] Wrong: "The program runs forever, so the time complexity is infinite or undefined."
[OK] Correct: We measure time complexity by how work grows with input size, not by total runtime. Here, each cycle does a fixed amount of work, so complexity grows linearly with cycles.
Understanding how loops that run repeatedly affect time helps you explain how programs behave over time, a useful skill in automation and control systems.
What if we added a nested loop inside the sequence that repeated a fixed number of times? How would the time complexity change?
Practice
Solution
Step 1: Understand traffic light function
Traffic lights manage traffic by showing red, yellow, and green lights in a set order.Step 2: Identify PLC role
PLC programs control the timing and sequence of these lights to keep traffic safe and flowing.Final Answer:
To control the order and timing of red, yellow, and green lights -> Option AQuick Check:
Traffic light sequence = control lights order and timing [OK]
- Confusing traffic light sequence with vehicle speed measurement
- Thinking traffic lights detect weather
- Assuming traffic lights count cars
Solution
Step 1: Identify the green light state
The green light state should be named clearly as GREEN to represent that light.Step 2: Match option to green state
Only state = GREEN assigns the state variable to GREEN, which is correct for the green light.Final Answer:
state = GREEN -> Option AQuick Check:
Green light state = GREEN [OK]
- Using RED or YELLOW for green light state
- Using OFF which means no light
- Confusing state names
timer = 0 state = 'RED' if timer < 10: light = 'RED' elif timer < 15: light = 'GREEN' else: light = 'YELLOW'
What will be the value of
light when timer is 12?Solution
Step 1: Check timer conditions for 12
12 is not less than 10, so first if is false. 12 is less than 15, so second condition is true.Step 2: Determine light value
Since timer < 15 is true, light is set to 'GREEN'.Final Answer:
'GREEN' -> Option CQuick Check:
timer=12 <15 -> light='GREEN' [OK]
- Choosing 'RED' because timer is close to 10
- Choosing 'YELLOW' by ignoring conditions order
- Thinking code causes error
state = 'RED' if state == 'RED': state = 'GREEN' elif state == 'GREEN': state = 'YELLOW' elif state == 'YELLOW': state = 'RED'
What is the main problem?
Solution
Step 1: Analyze state change logic
The code changes state immediately but only once; no loop or repeated check.Step 2: Identify missing repeated execution
Without looping or timer, state changes once and stops, so no cycling happens.Final Answer:
The state changes only once because conditions are checked once -> Option BQuick Check:
Single run code = no cycling [OK]
- Thinking variable name is wrong
- Believing comparison operators are incorrect
- Ignoring need for timing or looping
Solution
Step 1: Understand timing requirements
Each light needs a specific duration before switching to the next.Step 2: Choose best control method
A state machine with a timer that resets after each state's time allows precise control and cycling.Final Answer:
Use a state machine with a timer that resets after each state duration -> Option DQuick Check:
State machine + timer reset = correct timing control [OK]
- Using one timer without reset causes wrong timing
- Random delays break predictable sequence
- No state variable causes confusion in sequence
Ready to prove it?
Pass the test and this pattern moves unknown
