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
Photo by Brett Jordan on Unsplash

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.

# 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)
# Pause before closing
This shape has a fixed, constant diameter

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

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)

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

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!

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