Weekly Problem: Traffic

The Rush Hour Puzzle

🚦 The Problem

You’re managing a busy intersection where cars arrive from the north and east. Each direction gets a 30-second green light. Your goal is to decide which direction should get the green light next to minimize the total waiting time.

Simple Rules:

  • Cars take 3 seconds to cross the intersection
  • New cars arrive randomly
  • Each green light lasts exactly 30 seconds
  • You must decide which direction gets the next green light

North Queue

0 cars

Avg wait: 0s

East Queue

0 cars

Avg wait: 0s

📝 Your Task

Create a simple algorithm that decides which direction should get the next green light. Your goal is to:

  1. Keep the maximum queue length under 10 cars
  2. Ensure no car waits longer than 90 seconds
  3. Minimize the average waiting time for all cars

Write a function that:

function decideNextGreenLight(northQueue, eastQueue, northWaitTime, eastWaitTime) {
    // Your code here
    // Return 'north' or 'east'
}
            

Posted

in

,

by