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'
}
            
Weekly Problem: Traffic

Yildiz Culcu


Hi, I'm Yildiz Culcu, a student of Computer Science and Philosophy based in Germany. My mission is to help people discover the joy of learning about science and explore new ideas. As a 2x Top Writer on Medium and an active voice on LinkedIn, and this blog, I love sharing insights and sparking curiosity. I'm an emerging Decision science researcher associated with the Max Planck Institute for Cognitive and Brain Sciences and the University of Kiel. I am also a Mentor, and a Public Speaker available for booking. Let's connect and inspire one another to be our best!


Post navigation