Editorial for Hollow Pyramid

Problem of the Week 1b

Amos Aidoo
Nov 3 · 2 min read
Photo by The World Hopper from Pexels

The problem statement:

A hollow pyramid is a pyramid with walls and an empty space in it. Your friend James wants to know how a hollow pyramid will look like when cut from the top to bottom. Can you help him out?


This problem tests your understanding of loops. Let’s look at an example. If our input is 5, we can develop a table that looks like our output.

+---+---+---+---+---+---+---+---+---+
| | | | | * | | | | |
+---+---+---+---+---+---+---+---+---+
| | | | * | | * | | | |
+---+---+---+---+---+---+---+---+---+
| | | * | | | | * | | |
+---+---+---+---+---+---+---+---+---+
| | * | | | | | | * | |
+---+---+---+---+---+---+---+---+---+
| * | | * | | * | | * | | * |
+---+---+---+---+---+---+---+---+---+

From the table above, we can make some observations:

  1. A cell that doesn’t have an asterisk has space.

After these observations, we can write our code. The pseudocode is provided below:

input n
spaces = n - 1 //Number of spaces to print before the first asterisk
for row = 1 to n
//Print spaces
for i = 1 to spaces
print " "
end for

for i = 1 to row
if (row = n)
//Print only asterisks on the last line
print "* "
else if (i = 1 || i = row)
//Print an asterisk at both ends
print "* "
else
//Print spaces in between asterisks
print " "
end if
end for
end for

Do you have another approach to the problem, feel free to share your approach in the comments below. 😃

CSS Knust

Computer Science students from KNUST expressing themselves through writing on various topics.

Amos Aidoo

Written by

I found goodness in computer science.

CSS Knust

CSS Knust

Computer Science students from KNUST expressing themselves through writing on various topics.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade