DEV Community

Cover image for Seeing Code in the Real World: The Bus Seating Problem ๐ŸšŒ
Somnath Das
Somnath Das

Posted on

Seeing Code in the Real World: The Bus Seating Problem ๐ŸšŒ

Working in software development, it is hard to turn off the logic center of the brain. Have you ever looked at a completely normal, everyday situation and thought, "Wait, there's an algorithm for this"?

That happened to me recently on a simple bus ride.

I observed a family getting on the bus and trying to figure out their seating arrangement. Because of the way they sat, one child was left sitting alone. My brain immediately started crunching the numbersโ€”mathematically, there was absolutely a way to seat them all safely!

I decided to turn this real-world observation into a custom coding problem. I call it The Bus Seating Problem.

๐Ÿ“ The Scenario & Rules

Imagine an empty bus with paired seats. A group boards consisting of n adults (parents) and m children.

To make this a solid algorithmic challenge, let's establish some strict baseline constraints:

  • Both n and m are greater than 1 (at least 2 adults and 2 children).
  • The children always outnumber the adults (m > n).

Here are the survival rules for seating:

  • Two children can sit together safely.
  • No two adults can sit together.
  • A child CANNOT sit alone. They must sit with another child or an adult.
  • Adults can sit with children.

The Objective: Given n and m, is it possible to seat everyone safely?

Solutions : https://github.com/buildwithsomnath/everyday-algorithms

Top comments (0)