Facebook Pixel

Advanced Hollow Patterns (Hollow Pyramid)

Master the intersection of multi-inner-loop geometry and coordinate boundaries by building a complex hollow pyramid.

The Final Geometry Test

We know how to make a solid pyramid (using spaces and stars loops). We know how to make a hollow square (using coordinate boundaries). A Hollow Pyramid requires combining both techniques.

The Problem

If N = 4:

   *
  * *
 *   *
*******

The Analysis

This is a centered pyramid, so we must use an inner loop for leading spaces.

for (int space = 1; space <= n - row; space++) print(" ");

Now, we need the star loop. For a solid pyramid, it runs from 1 to (2 * row) - 1. But since it's hollow, we only print a star if it is the boundary. What are the boundaries of this specific row's star loop?

  1. The first position: col == 1
  2. The last position: col == (2 * row) - 1
  3. The bottom row: row == n

The Implementation

int n = 4;
for (int row = 1; row <= n; row++) {
    
    // Print leading spaces
    for (int space = 1; space <= n - row; space++) {
        System.out.print(" ");
    }
    
    // Print hollow stars
    int maxCols = (2 * row) - 1;
    for (int col = 1; col <= maxCols; col++) {
        // Coordinate Boundary Check
        if (col == 1 || col == maxCols || row == n) {
            System.out.print("*");
        } else {
            System.out.print(" "); // The hollow inside
        }
    }
    System.out.println();
}

The Takeaway

Advanced hollow shapes are solved in two layers:

  1. Use standard loops to navigate to the correct geometric space (handling leading invisible space).
  2. Use an internal loop with coordinate boundaries to draw the hollow outline. Mastering this means you have complete control over terminal 2D rendering.

First, use an inner loop to print the required leading spaces to align the shape. Then, use a second inner loop with if-else boundary checks to draw the hollow interior.

Inside the star loop, the boundaries are the first column (col == 1), the last column (col == 2*row - 1), and the final row (row == N).

Because once the leading spaces have aligned the shape, the hollow center of the pyramid must be filled with blank spaces to push the right-side stars to their correct positions.

Yes, you can use pure coordinate logic on a full rectangular grid, but calculating the diagonal boundaries mathematically is often much harder than the multi-loop approach.

The bottom row will print exactly like the middle rows a single star on the left, empty space, and a single star on the right leaving the bottom of the pyramid open.

Please Login.
Please Login.
Please Login.
Please Login.