Hollow Pyramid
Java coding interview problem for Pattern Printing: Hollow Pyramid.
The Hollow Pyramid Pattern is one of the most frequently asked star pattern programs in Java coding interviews.
Unlike a Full Pyramid, where every position inside the pyramid is filled with stars (*), a Hollow Pyramid prints stars only on its boundary, leaving the inside empty.
This pattern helps developers understand:
- Nested loops
- Boundary conditions
- Conditional statements
- Pattern visualization
- Row and column relationships
Mastering the Hollow Pyramid also makes it easier to solve advanced interview questions such as:
- Hollow Diamond
- Hollow Inverted Pyramid
- Hollow Butterfly
- Hollow Square
- Hollow Rectangle
What is a Hollow Pyramid?
A Hollow Pyramid is a triangular pattern where:
- The first row contains a single star.
- The left boundary contains stars.
- The right boundary contains stars.
- The last row is completely filled with stars.
- Every inner position contains spaces.
Example
*
* *
* *
* *
***************
Notice:
- Only the borders are printed.
- The center is hollow.
- The bottom row is completely filled.
Difference Between Full Pyramid and Hollow Pyramid
| Full Pyramid | Hollow Pyramid |
|---|---|
| All positions contain stars. | Only boundary positions contain stars. |
| Uses only nested loops. | Uses nested loops with conditions. |
| No interior spaces. | Interior is filled with spaces. |
| Simpler logic. | Requires boundary checking. |
Example
Full Pyramid
*
* * *
* * * * *
* * * * * * *
Hollow Pyramid
*
* *
* *
* *
***************
Why is Hollow Pyramid Asked in Interviews?
Interviewers use this problem to evaluate whether candidates can:
- Write nested loops correctly.
- Apply conditional logic.
- Understand boundary conditions.
- Print spaces accurately.
- Convert a filled pattern into a hollow pattern.
It also tests a candidate's ability to visualize output before writing code.
Understanding Boundary Conditions
The key idea behind a Hollow Pyramid is identifying where stars should be printed.
A star is printed only when one of the following conditions is true:
- First column of the pyramid.
- Last column of the pyramid.
- First row.
- Last row.
Otherwise,
print spaces.
Example
*
* *
* *
* *
***************
Only the edges contain stars.
Mathematical Pattern
Suppose
Rows = 5
Then
Number of Rows
5
Number of Columns
The base width is
2 × Rows − 1
Example
Rows = 5
Width
=
2 × 5 − 1
=
9
Leading Spaces
The first row starts with
Rows − 1
spaces.
Each new row reduces one leading space.
Example
| Row | Leading Spaces |
|---|---|
| 1 | 4 |
| 2 | 3 |
| 3 | 2 |
| 4 | 1 |
| 5 | 0 |
Stars
- Row 1 → One star
- Middle rows → Two stars
- Last row → Entire row filled
Visual Representation
For
Rows = 5
Row 1
*
-----------------------
Row 2
* *
-----------------------
Row 3
* *
-----------------------
Row 4
* *
-----------------------
Row 5
***************
Observe:
- Left edge contains stars.
- Right edge contains stars.
- Last row contains stars everywhere.
Pattern Output
Input
Rows = 5
Output
*
* *
* *
* *
***************
Understanding the Logic
There are three major steps.
Step 1
Print leading spaces.
Example
Row 1
Spaces
4
Step 2
Print stars or spaces.
Print a star only when
- First position
- Last position
- Last row
Otherwise,
print spaces.
Step 3
Move to the next line.
Repeat until all rows are completed.
Algorithm
Step 1
Read the number of rows.
rows = 5;
Step 2
Start the outer loop.
1
↓
Rows
Each iteration prints one row.
Step 3
Print leading spaces.
Rows − Current Row
Step 4
Print the pyramid columns.
The total columns are
2 × Current Row − 1
Step 5
Print a star when:
Column == 1
OR
Column == Last Column
OR
Current Row == Last Row
Otherwise,
print spaces.
Step 6
Move to the next line.
Repeat until all rows are completed.
Dry Run
Input
Rows = 4
Row 1
Leading spaces
3
Output
*
Row 2
Leading spaces
2
Output
* *
Row 3
Leading spaces
1
Output
* *
Row 4
Output
*******
Final Output
*
* *
* *
*******
Approach 1 — Using Nested Loops and Boundary Conditions
This is the standard interview solution.
The idea is simple:
- Print leading spaces.
- Print stars only on the boundary.
- Print spaces inside the pyramid.
- Fill the last row completely.
Complete Java Program
public class HollowPyramid {
public static void main(String[] args) {
int rows = 5;
for (int i = 1; i <= rows; i++) {
// Print leading spaces
for (int j = i; j < rows; j++) {
System.out.print(" ");
}
// Print pyramid
for (int j = 1; j <= (2 * i - 1); j++) {
if (j == 1 ||
j == (2 * i - 1) ||
i == rows) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
}
}
Output
*
* *
* *
* *
*********
Step-by-Step Code Explanation
Step 1
Declare the number of rows.
int rows = 5;
Step 2
Create the outer loop.
for (int i = 1; i <= rows; i++)
Each iteration prints one row.
Step 3
Print leading spaces.
for (int j = i; j < rows; j++)
This centers the pyramid.
Step 4
Print pyramid columns.
for (int j = 1; j <= (2 * i - 1); j++)
Each row contains
2 × Row − 1
positions.
Step 5
Check boundary conditions.
if (j == 1 ||
j == (2 * i - 1) ||
i == rows)
If true:
System.out.print("*");
Otherwise:
System.out.print(" ");
This creates the hollow effect.
Example Execution
Input
Rows = 5
Output
*
* *
* *
* *
*********
Why Does This Work?
The algorithm treats every row independently.
- The outer loop controls the number of rows.
- The first inner loop prints leading spaces to center the pyramid.
- The second inner loop prints the pyramid width (
2 × row − 1columns). - A conditional statement ensures that stars are printed only on the left edge, right edge, and bottom row.
- Every other position is filled with spaces, creating the hollow interior.
This combination of nested loops and boundary conditions produces a perfectly centered Hollow Pyramid.
Advantages of This Approach
- Easy to understand and implement.
- Demonstrates nested loops and conditional logic.
- Frequently asked in Java interviews.
- Easy to extend into Hollow Diamond and Hollow Inverted Pyramid.
- Uses only O(1) extra space.
Drawbacks
Although this is the standard interview solution, interviewers often ask follow-up questions such as:
- Can you create a reusable
printHollowPyramid()method? - Can you print a Hollow Inverted Pyramid?
- Can you combine both to create a Hollow Diamond?
- What is the time complexity?
- How would you modify it for number or alphabet patterns?
In Part 2, we'll cover:
- Optimized Approach
- Reusable
printHollowPyramid()Method - Hollow Inverted Pyramid
- Hollow Diamond
- Hollow Number Pyramid
- Time & Space Complexity
- Comparison of Approaches
- Common Interview Mistakes
- Interview Follow-up Questions
- Related Pattern Problems
- Key Takeaways
- Interview Tips
Approach 2 — Optimized Approach
The nested-loop solution presented in Part 1 is already the optimal approach for printing a Hollow Pyramid.
Since every position in the pyramid must be visited exactly once, there is no algorithm that can print the pattern in fewer operations.
The optimization focuses on:
- Cleaner code
- Better readability
- Reusable methods
- Easy modification for other hollow patterns
Optimized Java Program
public class HollowPyramidOptimized {
public static void main(String[] args) {
int rows = 5;
for (int row = 1; row <= rows; row++) {
// Print leading spaces
for (int space = 1; space <= rows - row; space++) {
System.out.print(" ");
}
// Print stars and inner spaces
for (int col = 1; col <= (2 * row - 1); col++) {
if (row == rows ||
col == 1 ||
col == (2 * row - 1)) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
}
}
Output
*
* *
* *
* *
*********
Approach 3 — Using a Reusable Method
Instead of placing all logic inside the main() method, create a reusable function.
This improves:
- Code readability
- Reusability
- Maintainability
- Unit testing
Java Program
public class HollowPyramidMethod {
static void printHollowPyramid(int rows) {
for (int row = 1; row <= rows; row++) {
// Leading spaces
for (int space = 1; space <= rows - row; space++) {
System.out.print(" ");
}
// Pyramid
for (int col = 1; col <= (2 * row - 1); col++) {
if (row == rows ||
col == 1 ||
col == (2 * row - 1)) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
}
public static void main(String[] args) {
printHollowPyramid(5);
}
}
Pattern Variation 1 — Hollow Inverted Pyramid
The pyramid is printed upside down while maintaining the hollow interior.
Output
*********
* *
* *
* *
*
Java Program
public class HollowInvertedPyramid {
public static void main(String[] args) {
int rows = 5;
for (int row = rows; row >= 1; row--) {
// Leading spaces
for (int space = rows; space > row; space--) {
System.out.print(" ");
}
// Pattern
for (int col = 1; col <= (2 * row - 1); col++) {
if (row == rows ||
row == 1 ||
col == 1 ||
col == (2 * row - 1)) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
}
}
Pattern Variation 2 — Hollow Diamond
A Hollow Diamond is formed by combining:
- Hollow Pyramid
- Hollow Inverted Pyramid
Output
*
* *
* *
* *
*********
* *
* *
* *
*
The upper half is a Hollow Pyramid.
The lower half is a Hollow Inverted Pyramid (excluding the middle row).
Pattern Variation 3 — Hollow Number Pyramid
Instead of stars, print numbers on the boundary.
Output
1
2 2
3 3
4 4
5 5 5 5 5 5 5 5 5
The same boundary logic applies; only the printed character changes.
Dry Run
Input
Rows = 4
Row 1
Leading spaces
3
Columns
*
Row 2
Leading spaces
2
Columns
* *
Row 3
Leading spaces
1
Columns
* *
Row 4
Since this is the last row, print stars in every column.
*******
Final Output
*
* *
* *
*******
Time Complexity
Suppose
n
is the number of rows.
Standard Hollow Pyramid
| Operation | Complexity |
|---|---|
| Time | O(n²) |
| Space | O(1) |
Hollow Inverted Pyramid
| Operation | Complexity |
|---|---|
| Time | O(n²) |
| Space | O(1) |
Hollow Diamond
Since it combines two pyramids,
| Operation | Complexity |
|---|---|
| Time | O(n²) |
| Space | O(1) |
Comparison of Approaches
| Approach | Time | Space | Recommended |
|---|---|---|---|
| Basic Nested Loops | O(n²) | O(1) | Best for Beginners |
| Reusable Method | O(n²) | O(1) | Production Ready |
| Hollow Inverted Pyramid | O(n²) | O(1) | Intermediate |
| Hollow Diamond | O(n²) | O(1) | Interview Favorite |
Common Mistakes
Mistake 1
Printing stars everywhere.
Wrong
System.out.print("*");
Correct
Print stars only on the boundary.
Mistake 2
Forgetting the last row condition.
Wrong
if (col == 1 || col == lastColumn)
Correct
if (row == rows ||
col == 1 ||
col == lastColumn)
Mistake 3
Using the wrong pyramid width.
Wrong
row
Correct
2 * row - 1
Mistake 4
Printing incorrect leading spaces.
Always print
rows - row
spaces.
Mistake 5
Using println() inside the inner loop.
Always use
System.out.println();
only after completing the entire row.
Interview Follow-up Questions
Q1. What is the difference between a Full Pyramid and a Hollow Pyramid?
Q2. Why are boundary conditions required?
Q3. How do you identify the left and right boundaries?
Q4. Why is the last row completely filled?
Q5. Can you create a Hollow Inverted Pyramid?
Q6. Can you combine two hollow pyramids into a Hollow Diamond?
Q7. What is the time complexity?
Q8. Can you print a Hollow Number Pyramid?
Q9. Can you replace stars with alphabets?
Q10. Can you generate the same pattern recursively?
Related Pattern Problems
- Full Pyramid
- Inverted Pyramid
- Diamond Pattern
- Hollow Diamond
- Hollow Rectangle
- Hollow Square
- Number Pyramid
- Pascal's Triangle
- Floyd's Triangle
Key Takeaways
- A Hollow Pyramid prints stars only on the boundary.
- The bottom row is completely filled with stars.
- The pyramid width for row
ris2 × r − 1. - Boundary conditions determine whether to print a star or a space.
- Nested loops combined with conditional statements are the key to solving this pattern.
- The algorithm runs in O(n²) time and uses O(1) extra space.
Frequently Asked Interview Questions
Q1. Why is the last row completely filled?
To close the hollow shape and complete the pyramid. Without a filled base, the pattern would appear incomplete.
Q2. How do we know when to print a star?
A star is printed when:
- It is the first column.
- It is the last column.
- It belongs to the last row.
All other positions are filled with spaces.
Q3. Why do we print leading spaces?
Leading spaces center the pyramid horizontally. Without them, the pattern becomes left-aligned.
Q4. Can this logic be reused for other hollow patterns?
Yes. The same boundary-checking technique is used for:
- Hollow Diamond
- Hollow Rectangle
- Hollow Square
- Hollow Butterfly
- Hollow Hourglass
Only the loop structure changes.
Q5. What is the difference between Full Pyramid and Hollow Pyramid?
| Full Pyramid | Hollow Pyramid |
|---|---|
| Every position contains a star. | Only boundary positions contain stars. |
| No conditional checks are needed. | Boundary conditions are required. |
| Interior is filled. | Interior is empty. |
Interview Tip
If an interviewer asks:
"Print a Hollow Pyramid in Java."
Start by explaining the three main steps:
- Print leading spaces to center the pyramid.
- Iterate through each column of the current row.
- Print a star only when the position lies on the left boundary, right boundary, or bottom row; otherwise, print a space.
After solving the basic problem, mention that the same boundary-condition logic can be extended to implement Hollow Inverted Pyramid, Hollow Diamond, Hollow Square, and Hollow Rectangle. Demonstrating this progression shows a deeper understanding of pattern programming and is often viewed favorably in technical interviews.