This is a design round. You write the class, and the interviewer checks that it behaves.
Here you build the controller of the traffic lights at a crossing. Two roads meet there: NS (north-south) and EW (east-west).
The lights repeat the same cycle forever. It has four phases, in this order:
- NS is green for
green_nsseconds. - NS is yellow for
yellowseconds. - EW is green for
green_ewseconds. - EW is yellow for
yellowseconds.
While one road is green or yellow, the other road is red.
Implement the class:
Crossing(green_ns, green_ew, yellow)starts the lights at time 0, when the NS green begins.tick(seconds)moves the time on byseconds.color(road)returns the colorroadshows now: "green", "yellow" or "red".can_go(road)returnsTrueonly whenroadis green. Yellow means stop.time_left()returns the seconds until the lights next change.wait_for(road)returns the seconds untilroadis green. It returns 0 whenroadis green now.
A change happens exactly on its second. So right after tick(green_ns), NS has just turned yellow. Then time_left() returns the full length of the yellow.
Every call except tick writes one line of output. Booleans are written as True and False.
Example 1:
Input:
Crossing(5, 3, 2)
1. color("NS")
2. color("EW")
3. time_left()
4. wait_for("EW")
5. tick(5)
6. color("NS")
7. can_go("NS")
8. time_left()
9. tick(2)
10. can_go("EW")
11. wait_for("NS")
Output:
green
red
5
7
yellow
False
2
True
5
Explanation:
NS is green from time 0 to 5, then yellow until 7.
EW is green from 7 to 10, then yellow until 12.
At time 0, NS stays green for 5 more seconds.
EW turns green at time 7, so call 4 returns 7.
Call 5 moves the time to 5. NS has just turned yellow.
It stays yellow for 2 more seconds.
Call 9 moves the time to 7, and EW turns green.
NS turns green again at time 12, when the next cycle starts.
Example 2:
Input:
Crossing(30, 20, 5)
1. tick(1000000000)
2. color("NS")
3. can_go("EW")
4. time_left()
5. wait_for("NS")
6. tick(17)
7. color("EW")
8. time_left()
9. wait_for("EW")
10. wait_for("NS")
Output:
red
True
15
20
yellow
3
38
3
Explanation:
One cycle lasts 30 + 5 + 20 + 5 = 60 seconds.
1,000,000,000 seconds are 16,666,666 cycles and 40 seconds.
So after call 1, the lights are 40 seconds into a cycle.
EW is green from 35 to 55 in each cycle.
Call 6 moves to 57 seconds into the cycle. EW is yellow until 60.
NS turns green when the next cycle starts, 3 seconds later.
EW turns green 35 seconds into that cycle, so in 3 + 35 = 38 seconds.
Example 3:
Input:
Crossing(1, 1, 1)
1. tick(1)
2. color("NS")
3. color("EW")
4. tick(1)
5. color("EW")
6. wait_for("EW")
7. tick(3)
8. color("NS")
9. wait_for("NS")
10. time_left()
Output:
yellow
red
green
0
yellow
3
1
Explanation:
Each phase lasts 1 second, so one cycle lasts 4 seconds.
At time 1, NS is yellow and EW is still red.
At time 2, EW is green, so call 6 returns 0.
Call 7 moves the time to 5. That is 1 second into the second cycle.
So NS is yellow again, for 1 more second.
NS turns green at time 8, 3 seconds later.
Constraints:
green_ns,green_ewandyelloware whole numbers from 1 to 100,000.secondsis a whole number from 1 to 1,000,000,000.roadis "NS" or "EW".- At most 100,000 calls follow the constructor.
Note:
Each call should run in O(1) time. Moving the time one second at a time would take O(seconds) time for each tick. One cycle lasts at most 400,000 seconds. Keep only the time inside the cycle, the remainder after each tick. Then every number fits in a 32-bit integer.
Free plan: 30 tutor messages. PRO is unlimited.
