Hollow Diamond
Java coding interview problem for Pattern Printing: Hollow Diamond.
The Hollow Diamond Pattern is one of the most popular advanced star pattern programs asked in Java coding interviews.
Unlike a Solid Diamond, where every position inside the diamond is filled with stars (*), a Hollow Diamond prints stars only on the outer boundary, leaving the inside empty.
Although the pattern looks complex, it is actually a combination of:
- Hollow Pyramid
- Hollow Inverted Pyramid
Understanding this pattern helps you master:
- Nested loops
- Boundary conditions
- Space management
- Pattern visualization
- Symmetry in programming
Once you understand the Hollow Diamond, many advanced interview patterns become much easier.
What is a Hollow Diamond?
A Hollow Diamond is a symmetric star pattern that forms the shape of a diamond.
Only the outer border contains stars.
Everything inside the boundary is empty.
Example
*
* *
* *
* *
* *
* *
* *
* *
*
Notice:
- Top contains one star.
- Middle is the widest part.
- Bottom mirrors the top.
- Interior contains spaces.
Difference Between Diamond and Hollow Diamond
| Solid Diamond | Hollow Diamond |
|---|---|
| Entire diamond is filled with stars. | Only the outer boundary contains stars. |
| Simple nested loops. | Nested loops with boundary conditions. |
| No interior spaces. | Interior is hollow. |
| Less conditional logic. | Requires boundary checking. |
Solid Diamond
*
***
*****
*******
*********
*******
*****
***
*
Hollow Diamond
*
* *
* *
* *
* *
* *
* *
* *
*
Why is Hollow Diamond Asked in Interviews?
Interviewers use this problem to evaluate whether candidates understand:
- Nested loops
- Complex conditional logic
- Pattern symmetry
- Space calculations
- Combining multiple patterns
The Hollow Diamond is often asked as a follow-up after:
- Full Pyramid
- Hollow Pyramid
- Inverted Pyramid
because it combines all three concepts.
Understanding Boundary Conditions
The key idea behind a Hollow Diamond is identifying where stars should be printed.
A star is printed only when:
- It is the first column of the current row.
- It is the last column of the current row.
- The row contains only one star.
Everything else is printed as spaces.
Example
*
* *
* *
* *
* *
* *
* *
* *
*
Only the boundary contains stars.
Mathematical Pattern
Suppose
Rows = 5
Upper Half
1
3
5
7
9
Lower Half
7
5
3
1
Observe
The width increases by
2
for every row until the middle.
Then decreases by
2
towards the bottom.
Leading Spaces
Upper Half
| Row | Spaces |
|---|---|
| 1 | 4 |
| 2 | 3 |
| 3 | 2 |
| 4 | 1 |
| 5 | 0 |
Lower Half
| Row | Spaces |
|---|---|
| 4 | 1 |
| 3 | 2 |
| 2 | 3 |
| 1 | 4 |
Visual Representation
For
Rows = 5
Row 1
*
--------------------
Row 2
* *
--------------------
Row 3
* *
--------------------
Row 4
* *
--------------------
Row 5
* *
--------------------
Row 6
* *
--------------------
Row 7
* *
--------------------
Row 8
* *
--------------------
Row 9
*
Notice the perfect symmetry.
Pattern Output
Input
Rows = 5
Output
*
* *
* *
* *
* *
* *
* *
* *
*
Understanding the Logic
The Hollow Diamond consists of two separate parts.
Part 1
Print the Hollow Pyramid.
Example
*
* *
* *
* *
* *
Part 2
Print the Hollow Inverted Pyramid.
Example
* *
* *
* *
*
Together they form a Hollow Diamond.
Algorithm
Step 1
Read the number of rows.
rows = 5;
Step 2
Print the upper half.
For every row
- Print leading spaces.
- Print stars only on the boundary.
Step 3
Print the lower half.
Start from
rows - 1
Repeat the same logic.
Step 4
Move to the next line.
Repeat until the diamond is complete.
Dry Run
Input
Rows = 3
Upper Half
*
* *
* *
Lower Half
* *
*
Final Output
*
* *
* *
* *
*
Approach 1 — Using Two Nested Loop Sections
This is the standard interview solution.
The first section prints the upper half.
The second section prints the lower half.
Complete Java Program
public class HollowDiamond {
public static void main(String[] args) {
int rows = 5;
// Upper Half
for (int i = 1; i <= rows; i++) {
// Leading spaces
for (int j = i; j < rows; j++) {
System.out.print(" ");
}
// Stars and inner spaces
for (int j = 1; j <= (2 * i - 1); j++) {
if (j == 1 ||
j == (2 * i - 1)) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
// Lower Half
for (int i = rows - 1; i >= 1; i--) {
// Leading spaces
for (int j = rows; j > i; j--) {
System.out.print(" ");
}
// Stars and inner spaces
for (int j = 1; j <= (2 * i - 1); j++) {
if (j == 1 ||
j == (2 * i - 1)) {
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
Print the upper half.
for (int i = 1; i <= rows; i++)
Each iteration prints one row of the upper diamond.
Step 3
Print leading spaces.
for (int j = i; j < rows; j++)
This centers the diamond.
Step 4
Print the diamond width.
2 * i - 1
columns.
Step 5
Print stars only on the boundary.
if (j == 1 ||
j == (2 * i - 1))
Otherwise,
System.out.print(" ");
Step 6
Print the lower half.
for (int i = rows - 1; i >= 1; i--)
This mirrors the upper half.
Example Execution
Input
Rows = 4
Output
*
* *
* *
* *
* *
* *
*
Why Does This Work?
The Hollow Diamond is created by combining two symmetric patterns:
- Upper Hollow Pyramid
- Lower Hollow Inverted Pyramid
Each half uses:
- An outer loop to control the rows.
- A loop for leading spaces to maintain center alignment.
- A loop for the diamond width (
2 × row − 1). - A boundary check to print stars only at the first and last positions of each row.
Because the lower half mirrors the upper half, the complete output forms a perfectly symmetric Hollow Diamond.
Advantages of This Approach
- Easy to understand.
- Demonstrates nested loops and boundary conditions.
- Frequently asked in coding interviews.
- Easy to modify into Hollow Butterfly or Hollow Hourglass.
- 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
printHollowDiamond()method? - Can you print a Hollow Number Diamond?
- Can you print an Alphabet Diamond?
- Can you generate the pattern using a single loop structure?
- What is the time complexity?
- How would you modify it for even-sized diamonds?
In Part 2, we'll cover:
- Optimized Approach
- Reusable
printHollowDiamond()Method - Hollow Number Diamond
- Hollow Alphabet Diamond
- 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 solution presented in Part 1 is already the optimal approach for printing a Hollow Diamond.
Every character position in the output must be visited exactly once, so no algorithm can reduce the overall time complexity below the number of printed characters.
The optimization focuses on:
- Cleaner implementation
- Better readability
- Reusable methods
- Easier maintenance
- Extending the solution to other hollow patterns
Optimized Java Program
public class HollowDiamondOptimized {
public static void main(String[] args) {
int rows = 5;
// Upper Half
for (int row = 1; row <= rows; row++) {
// Leading spaces
for (int space = 1; space <= rows - row; space++) {
System.out.print(" ");
}
// Diamond
for (int col = 1; col <= (2 * row - 1); col++) {
if (col == 1 || col == (2 * row - 1)) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
// Lower Half
for (int row = rows - 1; row >= 1; row--) {
// Leading spaces
for (int space = 1; space <= rows - row; space++) {
System.out.print(" ");
}
// Diamond
for (int col = 1; col <= (2 * row - 1); col++) {
if (col == 1 || col == (2 * row - 1)) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
}
}
Output
*
* *
* *
* *
* *
* *
* *
* *
*
Approach 3 — Using a Reusable Method
A reusable method improves:
- Readability
- Reusability
- Maintainability
- Unit Testing
Java Program
public class HollowDiamondMethod {
static void printHollowDiamond(int rows) {
// Upper Half
for (int row = 1; row <= rows; row++) {
for (int space = 1; space <= rows - row; space++) {
System.out.print(" ");
}
for (int col = 1; col <= (2 * row - 1); col++) {
if (col == 1 || col == (2 * row - 1)) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
// Lower Half
for (int row = rows - 1; row >= 1; row--) {
for (int space = 1; space <= rows - row; space++) {
System.out.print(" ");
}
for (int col = 1; col <= (2 * row - 1); col++) {
if (col == 1 || col == (2 * row - 1)) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
}
public static void main(String[] args) {
printHollowDiamond(5);
}
}
Pattern Variation 1 — Hollow Number Diamond
Instead of printing stars, print numbers on the boundary.
Output
1
2 2
3 3
4 4
5 5
4 4
3 3
2 2
1
Only the boundary values are printed.
Pattern Variation 2 — Hollow Alphabet Diamond
Instead of stars, print alphabets.
Output
A
B B
C C
D D
E E
D D
C C
B B
A
The boundary logic remains exactly the same.
Only the printed character changes.
Pattern Variation 3 — Hollow Diamond Using Two Methods
Instead of writing all logic in one method:
printUpperHalf();
printLowerHalf();
This makes the code cleaner and easier to maintain.
Pattern Variation 4 — Hollow Diamond with User Input
Scanner scanner = new Scanner(System.in);
System.out.print("Enter rows: ");
int rows = scanner.nextInt();
printHollowDiamond(rows);
This allows users to generate diamonds of any size.
Dry Run
Input
Rows = 3
Upper Half
Row 1
*
Row 2
* *
Row 3
* *
Lower Half
Row 2
* *
Row 1
*
Final Output
*
* *
* *
* *
*
Time Complexity
Suppose
n
is the number of rows.
Hollow Diamond
| Operation | Complexity |
|---|---|
| Time | O(n²) |
| Space | O(1) |
Reusable Method
| Operation | Complexity |
|---|---|
| Time | O(n²) |
| Space | O(1) |
Hollow Number Diamond
| 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 |
| Separate Upper & Lower Methods | O(n²) | O(1) | Best Code Organization |
| Number/Alphabet Diamond | O(n²) | O(1) | Interview Extension |
Common Mistakes
Mistake 1
Printing stars everywhere.
Wrong
System.out.print("*");
Correct
Print stars only when
col == 1
OR
col == lastColumn
Mistake 2
Printing the middle row twice.
Wrong
for (int row = rows; row >= 1; row--)
Correct
for (int row = rows - 1; row >= 1; row--)
Otherwise the widest row appears twice.
Mistake 3
Incorrect leading spaces.
Always print
rows - row
spaces.
Mistake 4
Using the wrong diamond width.
Correct
2 * row - 1
Mistake 5
Using println() inside the inner loop.
Always print
System.out.println();
after finishing the current row.
Interview Follow-up Questions
Q1. What is the difference between Diamond and Hollow Diamond?
Q2. Why is the lower half started from rows - 1?
Q3. Why are boundary conditions necessary?
Q4. Can you create the same pattern using recursion?
Q5. Can you print a Hollow Number Diamond?
Q6. Can you print an Alphabet Diamond?
Q7. Can you generate the pattern using methods?
Q8. What is the time complexity?
Q9. Can you print an even-sized diamond?
Q10. Can you combine Hollow Pyramid and Hollow Inverted Pyramid dynamically?
Related Pattern Problems
- Full Pyramid
- Hollow Pyramid
- Diamond Pattern
- Hollow Inverted Pyramid
- Hollow Square
- Hollow Rectangle
- Butterfly Pattern
- Pascal's Triangle
- Floyd's Triangle
Key Takeaways
- A Hollow Diamond is created by combining a Hollow Pyramid and a Hollow Inverted Pyramid.
- Only the outer boundary contains stars.
- The upper and lower halves are mirror images.
- Each row has a width of 2 × row − 1.
- Proper management of leading spaces and boundary conditions is the key to solving this pattern.
- The algorithm runs in O(n²) time with O(1) extra space.
Frequently Asked Interview Questions
Q1. Why do we start the lower half from rows - 1?
Because the middle row has already been printed by the upper half. Starting from rows - 1 avoids printing it twice.
Q2. Why are leading spaces required?
Leading spaces keep the diamond horizontally centered. Without them, the pattern becomes left-aligned and loses its diamond shape.
Q3. How do we identify the boundary?
For each row, print a star only when:
- It is the first column, or
- It is the last column of that row.
All other positions are printed as spaces.
Q4. Can the same logic be reused for other patterns?
Yes. The same boundary-condition approach can be extended to:
- Hollow Butterfly
- Hollow Hourglass
- Hollow Hexagon
- Hollow Number Diamond
- Hollow Alphabet Diamond
Only the loop structure or printed character changes.
Q5. What is the difference between a Solid Diamond and a Hollow Diamond?
| Solid Diamond | Hollow Diamond |
|---|---|
| Every position is filled with stars. | Only boundary positions contain stars. |
| No interior spaces. | Interior is hollow. |
| Simpler logic. | Requires conditional boundary checks. |
Interview Tip
If an interviewer asks:
"Print a Hollow Diamond in Java."
Explain that the solution is easier if you think of it as two independent patterns:
- A Hollow Pyramid for the upper half.
- A Hollow Inverted Pyramid for the lower half.
For each half:
- Print leading spaces to maintain alignment.
- Calculate the row width using
2 × row − 1. - Print a star only at the first and last positions of the current row.
- Print spaces everywhere else.
Finally, mention that this same strategy can be reused to build Hollow Number Diamonds, Alphabet Diamonds, Butterfly Patterns, and other advanced interview patterns. This demonstrates a strong understanding of pattern composition rather than memorizing individual solutions.