Python to the Rescue for Manhole Cover Interview Questions

Be prepared to surprise them during your next job interview!

Photo by Mohammad Rezaie on Unsplash

As the story goes, a bright young programmer interviewing at Google (or was it Microsoft? Or Facebook? … take your pick) was asked to explain why manhole covers are always round, and not square or some other shape.

The standard, very logical explanation is that round covers won’t fall through the hole they cover, taking into account the lips around the edges of the lid and the hole it snuggly fits into. Supposedly, round is the only shape where this is possible.

Photo by Brett Jordan on Unsplash

As a thought experiment, pick up a square manhole cover, and drop it diagonally on edge between the corners of the hole. But make sure nobody is down there first! Notice you can’t do this with a round cover, because the distance across the circular shape simply won’t fit down the hole, no matter how you turn it.

Yes, There Really Is Another Answer

Actually, there are an infinite number of unique manhole shapes that cannot fall through the hole they cover, no matter how they are turned. Some explanation is in order.

Python is my passion. It’s kind of my turn-to hammer for hacking my life in many fun ways. So, of course, I had to write a short Python program to draw the simplest new manhole cover shape, so I can explain it. Here’s the resulting code:

# manhole.pyfrom graphics import *
import math
# Shortcuts to math functions
sin = math.sin
cos = math.cos
rad = math.radians
deg = math.degrees
# Create a graphics window
win = GraphWin("Manhole Cover", 500, 500)
# Set window coordinates - bottom left to upper right
win.setCoords(-10, -10, 10, 10)
# Draw odd number of arcs
arcs = 3
for arc in range(arcs):
central_angle = arc * 360 / arcs
radius = 7
arc_radius = 2 * radius * sin(rad(180 - 180 / arcs) / 2)
xc = radius * cos(rad(central_angle))
yc = radius * sin(rad(central_angle))
for arc_angle in range(180 // arcs):
a1 = rad(central_angle + 180 - 90 / arcs + arc_angle)
a2 = rad(central_angle + 181 - 90 / arcs + arc_angle)
p1 = Point(xc + arc_radius * cos(a1), yc + arc_radius * sin(a1))
p2 = Point(xc + arc_radius * cos(a2), yc + arc_radius * sin(a2))
line_seg = Line(p1, p2)
line_seg.setWidth(2)
line_seg.draw(win)
# Pause before closing
win.getMouse()
win.close()

You don’t need to know exactly how this code works — understanding the figure it creates is the important concept here. When you run this code, a simple graphic window displays the new and improved manhole cover.

This shape has a fixed, constant diameter

It’s also easy to construct this geometric figure on good old fashioned paper with a compass. Start with three equidistant points — the corners of an equilateral triangle. Strike arcs centered on each point, running through the opposite points.

Constant Diameter

Notice that this shape has a fixed “diameter”, or the distance across its face from any one of its corner points to anywhere on the opposite arc. Now mentally pick this shape up and try to drop it down the manhole. Nope, it won’t fit, no matter which way you turn it!

Now We’re on a Roll

So, is there any advantage to making manhole covers in this shape? I like to think so. Imagine a worker accidentally letting a round manhole cover get away from him or her, with the heavy iron token rolling down the boulevard out of control. Yikes! The good news is, our new shape doesn’t roll near as easily. The top edge will stay the same height above the ground at all times as it rolls, but because of the shape, the center of gravity will oscillate from the center point of the shape as it rolls. It’s all a bit of magical physics, but the result is this new shape rests in a stable state on one edge, and hence it won’t roll away as easily.

But Wait, There’s More!

As long as you pick an odd number of equidistant points, you can construct yet another shape with this same constant-diameter feature. Near the center of the code listing above is a line where the variable named arcs is set to a value of 3. Set arcs to 5 and another valid manhole cover shape is drawn.

Any odd number of sides will work

Again, the distance from each corner point to all the points in the opposite arc is constant. It will not fall through a hole of the same shape.

Don’t Even Go There

The obvious question to ask is if a figure with 4, 6 or any even number of sides will work as a valid manhole cover. The answer is no. The program draws the figure just fine if you set arcs to an even number, but give 4 a try and notice the resulting shape doesn’t have that “constant diameter” property needed to protect workers down under the street somewhere.

Don’t try this shape at home! (Or out in the street)

True, the bulges on each side make it more difficult to drop down its own hole compared to a straight-sided square, but it can still fit diagonally down its hole it you rotate it and stand it on edge. It’s a definite no-go in the world of manhole covers.

What Goes Around Comes Around

So, any odd number of arcs, generated as described, will create a valid manhole cover that cannot drop down a hole of its own shape. Who knew! Just for fun, let’s try a bigger odd number, such as 9:

The bigger the odd number, the more circular the result

As you can see, with larger odd numbers the generated figures approach closer and closer to the shape of a circle. Technically, there’s an infinite number of manhole cover shapes, because there are in infinite number of odd numbers. Realistically, 3, or in an odd case or two maybe 5, is the maximum number of arcs to use, if you don’t want to resort to a simple round manhole.

We’ve Covered It All

If anyone ever sees a 3-sided manhole cover in the wild, let me know. I’m keeping my eyes out. Also, I’m collecting statistics and metrics on out-of-control round manhole covers. If you see any roll by, please advise!

John’s passion and mission is sharing Python code to help demystify life’s challenges and to have fun. John is the author of Python for Numworks ,
Python for OpenSCAD, and many other titles.

Author, inventor, entrepreneur — passionate about alternate energy, technology, and how Python programming can help you hack your life.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store