Diamond Pattern
Java coding interview problem for Pattern Printing: Diamond Pattern.
The Diamond Pattern is one of the most popular pattern programming questions asked in Java interviews.
Unlike simple triangle patterns, the Diamond Pattern combines two pyramid patterns:
- Full Pyramid
- Inverted Pyramid
Because it combines multiple concepts into one problem, interviewers use it to evaluate a candidate's understanding of:
- Nested loops
- Row and column relationships
- Space calculations
- Pattern decomposition
- Logical thinking
Once you master the Diamond Pattern, solving advanced patterns such as Butterfly Pattern, Hourglass Pattern, and Hollow Diamond Pattern becomes much easier.
What is a Diamond Pattern?
A Diamond Pattern is formed by combining:
- A Full Pyramid
- An Inverted Pyramid
Example
*
***
*****
*******
*********
*******
*****
***
*
The upper half increases the number of stars.
The lower half decreases the number of stars.
Together they create a diamond.
Why Are Diamond Pattern Programs Asked in Interviews?
Interviewers rarely ask Diamond Patterns because they want the output.
Instead, they want to evaluate whether you can:
- Break a large problem into smaller parts
- Reuse existing logic
- Work with nested loops
- Calculate spaces correctly
- Write clean and maintainable code
Most experienced developers solve this by identifying that the problem is simply:
Diamond
=
Full Pyramid
+
Inverted Pyramid
Recognizing this relationship is often more important than memorizing the code.
Understanding Rows, Spaces, and Stars
Suppose
Rows = 5
Output
*
***
*****
*******
*********
*******
*****
***
*
The diamond consists of two sections.
Upper Half
| Row | Spaces | Stars |
|---|---|---|
| 1 | 4 | 1 |
| 2 | 3 | 3 |
| 3 | 2 | 5 |
| 4 | 1 | 7 |
| 5 | 0 | 9 |
Formula
Leading spaces
Rows − Current Row
Stars
2 × Current Row − 1
Lower Half
| Row | Spaces | Stars |
|---|---|---|
| 4 | 1 | 7 |
| 3 | 2 | 5 |
| 2 | 3 | 3 |
| 1 | 4 | 1 |
Formula
Leading spaces
Rows − Current Row
Stars
2 × Current Row − 1
Notice that the same formula is reused.
Only the direction of the loop changes.
Mathematical Formula
Upper Half
Leading Spaces
Rows − Current Row
Stars
2 × Current Row − 1
Lower Half
Leading Spaces
Rows − Current Row
Stars
2 × Current Row − 1
The logic remains identical.
The only difference is whether the rows increase or decrease.
Visual Representation
For
Rows = 5
Upper Half
Row 1
Spaces = 4
Stars = 1
*
--------------------
Row 2
Spaces = 3
Stars = 3
***
--------------------
Row 3
Spaces = 2
Stars = 5
*****
--------------------
Row 4
Spaces = 1
Stars = 7
*******
--------------------
Row 5
Spaces = 0
Stars = 9
*********
====================
Lower Half
Row 4
Spaces = 1
Stars = 7
*******
--------------------
Row 3
Spaces = 2
Stars = 5
*****
--------------------
Row 2
Spaces = 3
Stars = 3
***
--------------------
Row 1
Spaces = 4
Stars = 1
*
Pattern Output
Input
Rows = 5
Output
*
***
*****
*******
*********
*******
*****
***
*
Understanding the Logic
Instead of treating the Diamond Pattern as a completely new problem, divide it into two familiar patterns.
Step 1
Print the Full Pyramid.
*
***
*****
*******
*********
Step 2
Print the Inverted Pyramid.
*******
*****
***
*
Notice that the last row of the upper pyramid is not repeated.
Therefore, the second loop starts from:
Rows - 1
instead of
Rows
This avoids printing the center row twice.
Brute Force Approach
The simplest solution uses six nested loops.
Upper Half
- One loop for rows
- One loop for spaces
- One loop for stars
Lower Half
- One loop for rows
- One loop for spaces
- One loop for stars
Although there are six loops, only three execute for each row.
Algorithm
Step 1
Read the number of rows.
rows = 5;
Step 2
Print the upper pyramid.
For every row
- Print leading spaces.
- Print stars.
- Move to the next line.
Step 3
Print the lower pyramid.
Start from
rows - 1
For every row
- Print leading spaces.
- Print stars.
- Move to the next line.
Step 4
The combination of these two pyramids forms the Diamond Pattern.
Dry Run
Input
Rows = 3
Upper Half
Row 1
Spaces = 2
Stars = 1
*
---------------
Row 2
Spaces = 1
Stars = 3
***
---------------
Row 3
Spaces = 0
Stars = 5
*****
Lower Half
Row 2
Spaces = 1
Stars = 3
***
---------------
Row 1
Spaces = 2
Stars = 1
*
Final Output
*
***
*****
***
*
Approach 1 — Using Nested Loops
This is the standard interview solution and is the easiest to explain.
Complete Java Program
public class DiamondPattern {
public static void main(String[] args) {
int rows = 5;
// Upper Half
for (int i = 1; i <= rows; i++) {
// Print spaces
for (int j = rows; j > i; j--) {
System.out.print(" ");
}
// Print stars
for (int k = 1; k <= (2 * i - 1); k++) {
System.out.print("*");
}
System.out.println();
}
// Lower Half
for (int i = rows - 1; i >= 1; i--) {
// Print spaces
for (int j = rows; j > i; j--) {
System.out.print(" ");
}
// Print stars
for (int k = 1; k <= (2 * i - 1); k++) {
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 pyramid.
for (int i = 1; i <= rows; i++)
Iterations
1
2
3
4
5
Step 3
Print leading spaces.
for (int j = rows; j > i; j--)
Spaces printed
| Row | Spaces |
|---|---|
| 1 | 4 |
| 2 | 3 |
| 3 | 2 |
| 4 | 1 |
| 5 | 0 |
Step 4
Print stars.
for (int k = 1; k <= (2 * i - 1); k++)
Stars printed
| Row | Stars |
|---|---|
| 1 | 1 |
| 2 | 3 |
| 3 | 5 |
| 4 | 7 |
| 5 | 9 |
Step 5
Print the lower pyramid.
for (int i = rows - 1; i >= 1; i--)
Why rows - 1?
Because the center row has already been printed in the upper half.
If we start from rows, the widest row would appear twice.
Example Execution
Input
Rows = 4
Output
*
***
*****
*******
*****
***
*
Why Does This Work?
The Diamond Pattern is not a completely new algorithm. It is created by combining two previously learned patterns:
- Full Pyramid – builds the upper half by increasing the number of stars and decreasing the spaces.
- Inverted Pyramid – builds the lower half by decreasing the number of stars and increasing the spaces.
The same formulas are reused in both halves:
- Leading Spaces = Rows − Current Row
- Stars = 2 × Current Row − 1
The only difference is the direction of the outer loop. By starting the second loop from rows - 1, the middle row is printed only once, resulting in a perfectly symmetrical diamond.
Advantages of This Approach
- Easy to understand and explain.
- Reuses Full Pyramid and Inverted Pyramid logic.
- Excellent for practicing nested loops.
- Frequently asked in Java coding interviews.
- Forms the foundation for Hollow Diamond and Butterfly patterns.
- Uses only O(1) extra memory.
Drawbacks
Although this is the standard interview solution, interviewers often ask additional questions such as:
- Can you create a reusable
printDiamond()method? - Can you print a Hollow Diamond Pattern?
- Can you print a Number Diamond?
- Can you print an Alphabet Diamond?
- What is the time complexity?
- Can you generate the diamond using recursion?
In Part 2, we'll cover:
- Reusable
printDiamond()Method - Hollow Diamond Pattern
- Number Diamond Pattern
- Alphabet Diamond Pattern
- Time and Space Complexity
- Comparison of Approaches
- Common Interview Mistakes
- Interview Follow-up Questions
- Related Pattern Problems
- Key Takeaways
- Interview Tips
Approach 2 — Using a Reusable Method
Instead of writing the Diamond Pattern logic directly inside the main() method, we can create a reusable method.
This approach improves:
- Code readability
- Reusability
- Maintainability
- Unit testing
Java Program
public class DiamondPatternMethod {
static void printDiamond(int rows) {
// Upper Half
for (int i = 1; i <= rows; i++) {
// Print spaces
for (int j = rows; j > i; j--) {
System.out.print(" ");
}
// Print stars
for (int k = 1; k <= (2 * i - 1); k++) {
System.out.print("*");
}
System.out.println();
}
// Lower Half
for (int i = rows - 1; i >= 1; i--) {
// Print spaces
for (int j = rows; j > i; j--) {
System.out.print(" ");
}
// Print stars
for (int k = 1; k <= (2 * i - 1); k++) {
System.out.print("*");
}
System.out.println();
}
}
public static void main(String[] args) {
printDiamond(5);
}
}
Output
*
***
*****
*******
*********
*******
*****
***
*
Pattern Variation 1 — Hollow Diamond Pattern
Instead of printing stars at every position, print only the boundary of the diamond.
Output
*
* *
* *
* *
*********
* *
* *
* *
*
Java Program
public class HollowDiamondPattern {
public static void main(String[] args) {
int rows = 5;
// Upper Half
for (int i = 1; i <= rows; i++) {
for (int j = rows; j > i; j--) {
System.out.print(" ");
}
for (int k = 1; k <= (2 * i - 1); k++) {
if (k == 1 || k == (2 * i - 1) || i == rows) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
// Lower Half
for (int i = rows - 1; i >= 1; i--) {
for (int j = rows; j > i; j--) {
System.out.print(" ");
}
for (int k = 1; k <= (2 * i - 1); k++) {
if (k == 1 || k == (2 * i - 1)) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
}
}
Pattern Variation 2 — Number Diamond Pattern
Replace stars with numbers.
Output
1
121
12321
1234321
123454321
1234321
12321
121
1
Java Program
public class NumberDiamondPattern {
public static void main(String[] args) {
int rows = 5;
// Upper Half
for (int i = 1; i <= rows; i++) {
for (int j = rows; j > i; j--) {
System.out.print(" ");
}
for (int j = 1; j <= i; j++) {
System.out.print(j);
}
for (int j = i - 1; j >= 1; j--) {
System.out.print(j);
}
System.out.println();
}
// Lower Half
for (int i = rows - 1; i >= 1; i--) {
for (int j = rows; j > i; j--) {
System.out.print(" ");
}
for (int j = 1; j <= i; j++) {
System.out.print(j);
}
for (int j = i - 1; j >= 1; j--) {
System.out.print(j);
}
System.out.println();
}
}
}
Pattern Variation 3 — Alphabet Diamond Pattern
Replace stars with alphabets.
Output
A
ABA
ABCBA
ABCDCBA
ABCDEDCBA
ABCDCBA
ABCBA
ABA
A
Java Program
public class AlphabetDiamondPattern {
public static void main(String[] args) {
int rows = 5;
// Upper Half
for (int i = 1; i <= rows; i++) {
for (int j = rows; j > i; j--) {
System.out.print(" ");
}
for (char ch = 'A'; ch < 'A' + i; ch++) {
System.out.print(ch);
}
for (char ch = (char) ('A' + i - 2); ch >= 'A'; ch--) {
System.out.print(ch);
}
System.out.println();
}
// Lower Half
for (int i = rows - 1; i >= 1; i--) {
for (int j = rows; j > i; j--) {
System.out.print(" ");
}
for (char ch = 'A'; ch < 'A' + i; ch++) {
System.out.print(ch);
}
for (char ch = (char) ('A' + i - 2); ch >= 'A'; ch--) {
System.out.print(ch);
}
System.out.println();
}
}
}
Dry Run
Input
Rows = 3
Processing
Upper Half
Row 1
Spaces = 2
Stars = 1
*
---------------
Row 2
Spaces = 1
Stars = 3
***
---------------
Row 3
Spaces = 0
Stars = 5
*****
===============
Lower Half
Row 2
Spaces = 1
Stars = 3
***
---------------
Row 1
Spaces = 2
Stars = 1
*
Final Output
*
***
*****
***
*
Time Complexity
Suppose
n
is the number of rows.
Standard Diamond Pattern
| Operation | Complexity |
|---|---|
| Time | O(n²) |
| Space | O(1) |
Hollow Diamond Pattern
| Operation | Complexity |
|---|---|
| Time | O(n²) |
| Space | O(1) |
Number Diamond Pattern
| Operation | Complexity |
|---|---|
| Time | O(n²) |
| Space | O(1) |
Alphabet Diamond Pattern
| 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 Diamond | O(n²) | O(1) | Intermediate |
| Number Diamond | O(n²) | O(1) | Interview Favorite |
| Alphabet Diamond | O(n²) | O(1) | Interview Favorite |
Common Mistakes
Mistake 1
Printing the center row twice.
Wrong
for (int i = rows; i >= 1; i--)
Correct
for (int i = rows - 1; i >= 1; i--)
The widest row has already been printed in the upper half.
Mistake 2
Printing incorrect leading spaces.
Wrong
for (int j = 1; j <= rows; j++)
Correct
for (int j = rows; j > i; j--)
Mistake 3
Using the wrong star formula.
Wrong
k <= i
Correct
k <= (2 * i - 1)
Mistake 4
Forgetting to move to the next line.
Wrong
System.out.print();
Correct
System.out.println();
Mistake 5
Trying to write the entire diamond using a single complex loop.
It is much easier and cleaner to think of a diamond as:
Diamond
=
Full Pyramid
+
Inverted Pyramid
Interview Follow-up Questions
Q1. Why is the lower half started from rows - 1?
Q2. How can you print a Hollow Diamond?
Q3. Can you print a Number Diamond?
Q4. Can you print an Alphabet Diamond?
Q5. What is the time complexity?
Q6. Can you solve it using recursion?
Q7. Can you print the pattern using only one outer loop?
Q8. How is a Diamond Pattern related to a Full Pyramid?
Q9. Can you create a Butterfly Pattern from this logic?
Q10. Can you reuse the Pyramid methods instead of rewriting the loops?
Related Pattern Problems
- Full Pyramid Pattern
- Inverted Pyramid Pattern
- Hollow Pyramid
- Hollow Diamond
- Butterfly Pattern
- Hourglass Pattern
- Number Pyramid
- Alphabet Pyramid
- Pascal's Triangle
- Floyd's Triangle
Key Takeaways
- A Diamond Pattern is created by combining a Full Pyramid and an Inverted Pyramid.
- Both halves use the same formulas:
- Leading Spaces = Rows − Current Row
- Stars = 2 × Current Row − 1
- The lower half starts from
rows - 1to avoid printing the middle row twice. - Thinking of complex patterns as combinations of simpler patterns makes them easier to implement.
- The standard solution runs in O(n²) time and uses O(1) extra space.
Frequently Asked Interview Questions
Q1. Why do we split the Diamond Pattern into two halves?
The upper half and lower half follow different row directions. Splitting the logic keeps the code simple, readable, and easy to debug.
Q2. Why does the lower half start from rows - 1?
The widest row (middle row of the diamond) has already been printed in the upper half. Starting from rows - 1 prevents duplicate output.
Q3. Can we print numbers or alphabets instead of stars?
Yes.
Keep the loop structure unchanged and replace the star-printing logic with number or alphabet printing.
Q4. What is the difference between a Full Pyramid and a Diamond Pattern?
| Full Pyramid | Diamond Pattern |
|---|---|
| Only the upper half is printed. | Contains both upper and lower halves. |
| Rows increase only once. | Rows increase and then decrease. |
| Single triangle shape. | Symmetrical diamond shape. |
Q5. Can the Diamond Pattern be implemented using reusable methods?
Yes.
A clean design is to create separate methods such as:
printPyramid(rows);
printInvertedPyramid(rows - 1);
This improves modularity and avoids duplicating logic.
Interview Tip
If an interviewer asks:
"Print a Diamond Pattern in Java."
Don't treat it as a completely new problem. Explain that a Diamond Pattern is simply the combination of:
- A Full Pyramid
- An Inverted Pyramid
Reuse the same formulas for spaces and stars, and start the lower half from rows - 1 to avoid duplicating the center row. This demonstrates problem decomposition, code reuse, and strong logical thinking—qualities interviewers value in real-world software development.