- 1Print Right Triangle Star Pattern in Java
- 2Print Inverted Right Triangle Star Pattern
- 3Print Mirrored Right Triangle Star Pattern in Java
- 4Print Inverted Mirrored Right Triangle Star Pattern
- 5Print Right Pascal's Triangle Star Pattern in Java
- 6Print K Shape Star Pattern in Java
- 7Print Mirrored Right Pascal's Triangle Pattern in Java
- 8Print Full Reverse K Star Pattern in Java
- 9Print Pyramid Star Pattern in Java
- 10Print Inverted Pyramid Star Pattern in Java
- 11Print Diamond Star Pattern in Java
- 12Print Hollow Diamond Star Pattern in Java
- 13Print Sandglass Star Pattern in Java
- 14Print Butterfly Star Pattern in Java
Print Hollow Diamond Star Pattern in Java
Topic
In this Java interview program, we’ll learn how to print a hollow diamond star pattern. The hollow diamond pattern consists of stars forming a diamond shape, but with the middle area empty except for the outline. This pattern is a common way to assess a beginner’s ability to use nested loops and conditionals in Java.
Examples
Input: n = 5 (number of rows in the upper half, total rows = 2 * n - 1)
*
* *
* *
* *
* *
* *
* *
* *
*
Input: n = 3
*
* *
* *
* *
*
Edge Case: n = 1
*
Interviewer Expectations
The interviewer is checking your understanding of:
- Nested loops (outer and inner loops)
- Conditional logic to print stars at correct positions
- Mathematical reasoning to build symmetrical shapes
- Clean code practices and variable naming
Approach
The diamond pattern can be divided into two parts: the top half and the bottom half. Each part has spaces and stars.
- For each row, print leading spaces to align the stars symmetrically.
- Print a star at the beginning and end of the row.
- If it's not the first or last column in that row, print spaces in between to make it hollow.
- Repeat the above for both the upper and lower half of the diamond.
Dry Run (n = 3)
// Top half
Row 1: 2 spaces + 1 star → " *"
Row 2: 1 space + star + space + star → " * *"
Row 3: 0 spaces + star + 3 spaces + star → "* *"
// Bottom half (excluding the middle)
Row 1: 1 space + star + space + star → " * *"
Row 2: 2 spaces + 1 star → " *"
Java Program
public class HollowDiamondPattern {
public static void main(String[] args) {
int n = 5;
// Top half
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n - i; j++) {
System.out.print(" ");
}
System.out.print("*");
if (i != 1) {
for (int j = 1; j <= (2 * i - 3); j++) {
System.out.print(" ");
}
System.out.print("*");
}
System.out.println();
}
// Bottom half
for (int i = n - 1; i >= 1; i--) {
for (int j = 1; j <= n - i; j++) {
System.out.print(" ");
}
System.out.print("*");
if (i != 1) {
for (int j = 1; j <= (2 * i - 3); j++) {
System.out.print(" ");
}
System.out.print("*");
}
System.out.println();
}
}
}
*
* *
* *
* *
* *
* *
* *
* *
*
Possible Followup Questions with Answers
1. How do you print a filled diamond pattern instead of a hollow one?
Instead of printing spaces between the two stars, print stars continuously:
for (int j = 1; j <= (2 * i - 1); j++) {
System.out.print("*");
}
2. How can you print the pattern for an even number of rows?
The current logic works best for odd numbers to ensure symmetry. For even n, you can either:
- Reduce one row to make it odd
- Or adjust the logic to include two middle rows
3. How would you return the pattern as a string instead of printing?
Use a StringBuilder
and append each line, then return the full string.
StringBuilder sb = new StringBuilder();
sb.append(" *\n");
Comments
Loading comments...