December 5, 2021

The FencePost Error

Some friends and I were talking about the recent Dayton police lawsuit – where people are suing the city because only a small percentage of African American applicants pass the entrance exam. In order to squash concerns that the exam was somehow racist, the city released a bunch of sample questions from the exam. After reading it, I can confirm that it’s not really racist. It’s probably another case of correlation vs causation at work here. If less blacks take the test than whites, less blacks are going to pass the test.

Looking at the test though, some friends of mine and I kept staring at question #9. Question 9 reads:

A parade has been scheduled to run 5 miles through the city. It is desirable to have an officer stationed every 3 blocks. If there are 12 blocks in a mile, how many officers will be needed?

A) 13

B) 15

C) 18

D) 20

It’s easy to see the logic that they want you to do here. 12 blocks X 5 miles = 60 blocks. Divide that by 3 and you get 20.

20 seems like the right answer, until you actually think about how you’d implement this in the real world. In the real world you’d want to put an officer at the start and finish of the race, so you’d need 21 officers to work it, not 20. The logic that the question uses is not the logic you’d use if you were really trying to position police officers along a parade route.

It’s a classic example of what we computer scientists call the “fencepost” or “off by 1” problem.

The problem here is that you don’t want to count the number of 3 block sections, you want to count the number of officers that have 3 block sections between them.

It’s the difference between counting sections of fence or fence posts. Let’s look at a graphical example.

Assume this is a fence:

|—|—|—|—|

This fence has 4 sections, but needs 5 posts to connect them. So, if I want a 4 mile fence with 1 mile long sections, I’ll need 5 fence posts.

The same holds true with positioning police officers at a marathon.

The error most often occurs when counting starts at 1 rather than 0 – and it’s why we computer scientists always write things like:

for (i=0; i<5; i++) instead of for (i=1; i<6; i++) or, the similarily confusing for (i=1; i<=5; i++)

About Ryan Jones

Ryan Jones is an SEO from Detroit. By day he works as a manager of SEO & Analytics at SapientNitro where his team performs SEO for Fortune500 clients. By night he's either playing hockey or attempting to take over the world with his own websites - which he would have already succeeded in doing had it not been for those meddling kids and their dog. The views expressed here have not been paid for and belong only to Ryan, not any of his employers or clients. Follow Ryan on Twitter at: @RyanJones, add him on Google+ or visit his personal website: www.RyanMJones.com