Simulate Traffic Flow Through a City
Design a simulation for traffic flow through a simplified city section with roads and traffic lights. The simulation should operate in 1-minute intervals. In each interval, traffic lights may change, and cars move along roads. Each road segment takes 1 minute to traverse. Entry points are marked with letters (e.g., 'J', 'A', 'B'). Intersections have traffic lights.
Part 1: Simulate a Single Road (Road J)
- Data Model: Create a data model to represent roads, traffic lights, and cars.
- Simulation Function: Implement a function that simulates time in 1-minute intervals. This function should move a car along the road, stopping it at red lights and allowing it to proceed when lights turn green. The simulation should continue until the car exits the city.
- Traffic Light Control: For this part, use a simple traffic light control logic: both lights are green at minute 1, and then they toggle between red and green every minute thereafter.
Deliverable: The function should return the total time taken for a single car to travel from entry to exit, assuming it starts at minute 2 and encounters the described traffic light behavior.
Example Scenario:
- A car enters road J at minute 2.
- It takes 1 minute to reach the first light (A-J).
- If the light is green, it takes another minute to reach the second light (B-J).
- If that light is also green, it takes another minute to exit.
- Total travel time = 3 minutes (driving) + waiting time at lights.
- If all lights are green, the car exits at minute 5.
AI interview prep
Core dilemma
Accurately modeling discrete time-based events and state transitions for multiple entities (cars, lights) within a simulation framework, while managing dependencies and ensuring correct progression.
3 tradeoffs and 2 stress tests locked
Interview prep
Unlock the interviewer X-Ray rubric, tradeoff matrix, stress tests, clarifying questions, and key concepts.
Simulate Traffic Flow Through a City
Design a simulation for traffic flow through a simplified city section with roads and traffic lights. The simulation should operate in 1-minute intervals. In each interval, traffic lights may change, and cars move along roads. Each road segment takes 1 minute to traverse. Entry points are marked with letters (e.g., 'J', 'A', 'B'). Intersections have traffic lights.
Part 1: Simulate a Single Road (Road J)
- Data Model: Create a data model to represent roads, traffic lights, and cars.
- Simulation Function: Implement a function that simulates time in 1-minute intervals. This function should move a car along the road, stopping it at red lights and allowing it to proceed when lights turn green. The simulation should continue until the car exits the city.
- Traffic Light Control: For this part, use a simple traffic light control logic: both lights are green at minute 1, and then they toggle between red and green every minute thereafter.
Deliverable: The function should return the total time taken for a single car to travel from entry to exit, assuming it starts at minute 2 and encounters the described traffic light behavior.
Example Scenario:
- A car enters road J at minute 2.
- It takes 1 minute to reach the first light (A-J).
- If the light is green, it takes another minute to reach the second light (B-J).
- If that light is also green, it takes another minute to exit.
- Total travel time = 3 minutes (driving) + waiting time at lights.
- If all lights are green, the car exits at minute 5.