Editorial for Hollow Pyramid

Problem of the Week 1b

Amos Aidoo
CSS Knust
2 min readNov 3, 2019

--

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.
  2. The first line has 4 spaces before the first asterisk, the second has 3, the third has 2, the fourth has 1 and the last has none. We can draw some conclusions from here that is the input is n, we first print n-1 spaces on the first line, n-2 spaces on the second, … , n-n spaces on the last line.
  3. For each row, after we print out an asterisk, we print a space too. Subsequently, we print spaces in pairs until we print the last asterisk for the row.

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

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

--

--