Inside the ComputerProgramming

The Fencepost Puzzle

A 10-meter fence with a post every meter needs 11 posts

A 10-meter fence, a post every meter. Easy: 10 posts, right? Programmers fall into this exact trap all the time.

You'll find out

  • Where the extra post comes from
  • Why code counts from zero
  • The one-symbol bug in a loop

Why it matters

Off-by-one slips are a classic bug in code, and they hide in calendars, page counts and hotel stays too.

About 3 min6 interactive slides · 5 questionsLesson 25 · Zone 3

Play this lesson

Free to play in your browser or on iPhone

More in this zone

  1. 19ProbabilityShuffle Isn't RandomSpotify made shuffle less random because the real thing felt rigged
  2. 20Data StorageDeleted Isn't GoneDeleting a file usually just erases its address, not its contents
  3. 21GeometryThe Rope Around the EarthAdd one meter to a rope around the planet and it lifts 16 cm everywhere
  4. 22AttentionThe Invisible GorillaHalf of people miss a gorilla walking through a basketball game
  5. 23VideoThe Wagon-Wheel EffectWhy wheels spin backwards in videos
  6. 24GeometryThe Missing SquareRearrange the same pieces and a square appears from nowhere
  7. 26Exponential GrowthFold Paper to the MoonFold a sheet 42 times and it would reach the Moon
  8. 27AeronauticsHow Planes Really FlyThe famous ‘longer path over the top’ explanation is wrong