Butterfly Pattern
Java coding interview problem for Pattern Printing: Butterfly Pattern.
The Butterfly Pattern is one of the most interesting and frequently asked advanced pattern programs in Java coding interviews.
It combines symmetry, nested loops, space management, and pattern visualization into a single problem.
Unlike a simple pyramid or diamond, the Butterfly Pattern consists of two mirrored triangles separated by spaces.
Learning this pattern helps you master:
- Nested loops
- Mirror patterns
- Symmetry
- Space calculations
- Complex pattern construction
Once you understand the Butterfly Pattern, you can easily solve many advanced interview patterns.
What is a Butterfly Pattern?
A Butterfly Pattern consists of:
- Left Wing
- Right Wing
- Center Spaces
The upper half expands.
The lower half contracts.
Example
* *
** **
*** ***
**** ****
**********
**** ****
*** ***
** **
* *
Notice:
- Left stars increase.
- Right stars increase.
- Middle spaces decrease.
- Lower half mirrors the upper half.
The complete output resembles the wings of a butterfly.
Difference Between Diamond and Butterfly Pattern
| Diamond Pattern | Butterfly Pattern |
|---|---|
| Single centered shape. | Two mirrored triangles. |
| Stars expand from the center. | Stars expand from both sides. |
| One pattern section. | Two mirrored wings. |
| Center contains stars. | Center contains spaces. |
Diamond Pattern
*
***
*****
*******
*********
*******
*****
***
*
Butterfly Pattern
* *
** **
*** ***
**** ****
**********
**** ****
*** ***
** **
* *
Why is Butterfly Pattern Asked in Interviews?
Interviewers ask this problem because it combines multiple concepts into one solution.
It evaluates whether candidates understand:
- Nested loops
- Mirror symmetry
- Space calculations
- Complex loop relationships
- Pattern decomposition
It is often asked after candidates solve:
- Pyramid
- Diamond
- Hollow Pyramid
because Butterfly Pattern requires combining these ideas.
Understanding Symmetry
The Butterfly Pattern has two symmetric halves.
Upper Half
* *
** **
*** ***
**** ****
**********
Lower Half
**** ****
*** ***
** **
* *
The lower half is the mirror image of the upper half.
Mathematical Pattern
Suppose
Rows = 5
Upper Half
| Row | Left Stars | Middle Spaces | Right Stars |
|---|---|---|---|
| 1 | 1 | 8 | 1 |
| 2 | 2 | 6 | 2 |
| 3 | 3 | 4 | 3 |
| 4 | 4 | 2 | 4 |
| 5 | 5 | 0 | 5 |
Middle spaces follow the formula
2 × (Rows − Row)
Example
Rows = 5
Row = 3
Spaces
=
2 × (5 − 3)
=
4
Stars
The number of stars on both sides equals
Current Row
Visual Representation
For
Rows = 5
Row 1
* *
------------------
Row 2
** **
------------------
Row 3
*** ***
------------------
Row 4
**** ****
------------------
Row 5
**********
------------------
Row 6
**** ****
------------------
Row 7
*** ***
------------------
Row 8
** **
------------------
Row 9
* *
Observe:
- Left wing expands.
- Right wing expands.
- Center gap shrinks.
- Lower half mirrors the upper half.
Pattern Output
Input
Rows = 5
Output
* *
** **
*** ***
**** ****
**********
**** ****
*** ***
** **
* *
Understanding the Logic
The Butterfly Pattern has two sections.
Upper Half
Each row prints:
- Left stars
- Middle spaces
- Right stars
Example
Row 3
***
Spaces
4
***
Output
*** ***
Lower Half
The same logic is repeated in reverse.
Example
****
Spaces
2
****
Output
**** ****
Algorithm
Step 1
Read the number of rows.
rows = 5;
Step 2
Print the upper half.
For every row
- Print left stars.
- Print middle spaces.
- Print right stars.
Step 3
Print the lower half.
Start from
rows - 1
Repeat the same logic.
Step 4
Move to the next line.
Repeat until the butterfly is completed.
Dry Run
Input
Rows = 3
Upper Half
Row 1
*
Spaces
4
*
Output
* *
Row 2
**
Spaces
2
**
Output
** **
Row 3
***
***
Output
******
Lower Half
Row 2
** **
Row 1
* *
Final Output
* *
** **
******
** **
* *
Approach 1 — Using Two Nested Loop Sections
This is the standard interview solution.
The first section prints the upper butterfly.
The second section prints the lower butterfly.
Complete Java Program
public class ButterflyPattern {
public static void main(String[] args) {
int rows = 5;
// Upper Half
for (int i = 1; i <= rows; i++) {
// Left Wing
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
// Middle Spaces
for (int j = 1; j <= 2 * (rows - i); j++) {
System.out.print(" ");
}
// Right Wing
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
System.out.println();
}
// Lower Half
for (int i = rows - 1; i >= 1; i--) {
// Left Wing
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
// Middle Spaces
for (int j = 1; j <= 2 * (rows - i); j++) {
System.out.print(" ");
}
// Right Wing
for (int j = 1; j <= i; j++) {
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 butterfly.
Step 3
Print the left wing.
for (int j = 1; j <= i; j++)
The number of stars equals the current row.
Step 4
Print the middle spaces.
for (int j = 1; j <= 2 * (rows - i); j++)
The gap decreases by 2 spaces after each row.
Step 5
Print the right wing.
for (int j = 1; j <= i; j++)
The right wing mirrors the left wing.
Step 6
Print the lower half.
for (int i = rows - 1; i >= 1; i--)
This mirrors the upper half and completes the butterfly.
Example Execution
Input
Rows = 4
Output
* *
** **
*** ***
********
*** ***
** **
* *
Why Does This Work?
The Butterfly Pattern is built using two mirrored triangles.
The algorithm divides the pattern into:
- Upper Half
- Lower Half
For every row:
- The left wing prints stars equal to the current row.
- The center gap prints spaces using the formula
2 × (rows − currentRow). - The right wing prints the same number of stars as the left wing.
The lower half repeats the same logic in reverse order, producing a perfectly symmetrical butterfly.
Advantages of This Approach
- Easy to understand.
- Demonstrates nested loops and symmetry.
- Frequently asked in Java interviews.
- Can be extended into Hollow Butterfly and Number Butterfly.
- 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
printButterflyPattern()method? - Can you print a Hollow Butterfly Pattern?
- Can you print a Number Butterfly?
- Can you print an Alphabet Butterfly?
- What is the time complexity?
- Can you solve it using a single loop structure?
In Part 2, we'll cover:
- Optimized Approach
- Reusable
printButterflyPattern()Method - Hollow Butterfly Pattern
- Number Butterfly Pattern
- Alphabet Butterfly Pattern
- 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 Butterfly Pattern shown in Part 1 is already the optimal solution.
Since every character (star or space) must be printed exactly once, no algorithm can improve the overall time complexity beyond the total number of printed characters.
The optimization focuses on:
- Cleaner code
- Better readability
- Reusable methods
- Easier maintenance
- Extending the logic to other butterfly variations
Optimized Java Program
public class ButterflyPatternOptimized {
public static void main(String[] args) {
int rows = 5;
// Upper Half
for (int row = 1; row <= rows; row++) {
// Left Wing
for (int star = 1; star <= row; star++) {
System.out.print("*");
}
// Middle Spaces
for (int space = 1; space <= 2 * (rows - row); space++) {
System.out.print(" ");
}
// Right Wing
for (int star = 1; star <= row; star++) {
System.out.print("*");
}
System.out.println();
}
// Lower Half
for (int row = rows - 1; row >= 1; row--) {
// Left Wing
for (int star = 1; star <= row; star++) {
System.out.print("*");
}
// Middle Spaces
for (int space = 1; space <= 2 * (rows - row); space++) {
System.out.print(" ");
}
// Right Wing
for (int star = 1; star <= row; star++) {
System.out.print("*");
}
System.out.println();
}
}
}
Output
* *
** **
*** ***
**** ****
**********
**** ****
*** ***
** **
* *
Approach 3 — Using a Reusable Method
Instead of writing the entire logic inside main(), create a reusable method.
Benefits:
- Cleaner code
- Better modularity
- Easy testing
- Reusable in multiple programs
Java Program
public class ButterflyPatternMethod {
static void printButterflyPattern(int rows) {
// Upper Half
for (int row = 1; row <= rows; row++) {
for (int star = 1; star <= row; star++) {
System.out.print("*");
}
for (int space = 1; space <= 2 * (rows - row); space++) {
System.out.print(" ");
}
for (int star = 1; star <= row; star++) {
System.out.print("*");
}
System.out.println();
}
// Lower Half
for (int row = rows - 1; row >= 1; row--) {
for (int star = 1; star <= row; star++) {
System.out.print("*");
}
for (int space = 1; space <= 2 * (rows - row); space++) {
System.out.print(" ");
}
for (int star = 1; star <= row; star++) {
System.out.print("*");
}
System.out.println();
}
}
public static void main(String[] args) {
printButterflyPattern(5);
}
}
Pattern Variation 1 — Hollow Butterfly Pattern
Instead of filling the wings completely with stars, print stars only on the outer boundary.
Output
* *
** **
* * * *
* * * *
* ** *
* * * *
* * * *
** **
* *
Java Program
public class HollowButterfly {
public static void main(String[] args) {
int rows = 5;
// Upper Half
for (int row = 1; row <= rows; row++) {
// Left Wing
for (int col = 1; col <= row; col++) {
if (col == 1 || col == row) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
// Middle Spaces
for (int space = 1; space <= 2 * (rows - row); space++) {
System.out.print(" ");
}
// Right Wing
for (int col = 1; col <= row; col++) {
if (col == 1 || col == row) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
}
}
Pattern Variation 2 — Number Butterfly Pattern
Replace stars with numbers.
Output
1 1
12 21
123 321
1234 4321
1234554321
1234 4321
123 321
12 21
1 1
This variation is commonly asked in advanced pattern interviews.
Pattern Variation 3 — Alphabet Butterfly Pattern
Replace stars with alphabets.
Output
A A
AB BA
ABC CBA
ABCD DCBA
ABCDEEDCBA
ABCD DCBA
ABC CBA
AB BA
A A
The loop structure remains unchanged.
Only the printed value changes.
Pattern Variation 4 — User Input
Allow users to enter the number of rows.
Scanner scanner = new Scanner(System.in);
System.out.print("Enter rows: ");
int rows = scanner.nextInt();
printButterflyPattern(rows);
Dry Run
Input
Rows = 3
Upper Half
Row 1
*
Spaces
4
*
Output
* *
Row 2
**
Spaces
2
**
Output
** **
Row 3
***
***
Output
******
Lower Half
Row 2
** **
Row 1
* *
Final Output
* *
** **
******
** **
* *
Time Complexity
Suppose
n
is the number of rows.
Butterfly Pattern
| Operation | Complexity |
|---|---|
| Time | O(n²) |
| Space | O(1) |
Hollow Butterfly
| Operation | Complexity |
|---|---|
| Time | O(n²) |
| Space | O(1) |
Number Butterfly
| 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 Butterfly | O(n²) | O(1) | Interview Favorite |
| Number/Alphabet Butterfly | O(n²) | O(1) | Advanced Variation |
Common Mistakes
Mistake 1
Printing the middle row twice.
Wrong
for (int row = rows; row >= 1; row--)
Correct
for (int row = rows - 1; row >= 1; row--)
Mistake 2
Using an incorrect space formula.
Wrong
rows - row
Correct
2 * (rows - row)
The gap decreases by two spaces each row.
Mistake 3
Printing an unequal number of stars.
Always print:
Current Row
stars on both wings.
Mistake 4
Printing the right wing before the spaces.
Correct order:
- Left Wing
- Middle Spaces
- Right Wing
Mistake 5
Using println() inside an inner loop.
Always print:
System.out.println();
after completing an entire row.
Interview Follow-up Questions
Q1. What is the Butterfly Pattern?
Q2. Why are there two mirrored halves?
Q3. How do you calculate the middle spaces?
Q4. Why is the lower half started from rows - 1?
Q5. Can you print a Hollow Butterfly Pattern?
Q6. Can you print a Number Butterfly?
Q7. Can you print an Alphabet Butterfly?
Q8. What is the time complexity?
Q9. Can you generate the pattern using recursion?
Q10. Can you create a reusable method?
Related Pattern Problems
- Full Pyramid
- Hollow Pyramid
- Diamond Pattern
- Hollow Diamond
- Floyd's Triangle
- Pascal's Triangle
- Number Pyramid
- Alphabet Pyramid
- Hollow Rectangle
Key Takeaways
- The Butterfly Pattern consists of two mirrored triangles.
- Each row prints:
- Left wing
- Middle spaces
- Right wing
- The number of stars equals the current row.
- The number of middle spaces is calculated using
2 × (rows − row). - The lower half is the mirror image of the upper half.
- The algorithm runs in O(n²) time and requires O(1) extra space.
Frequently Asked Interview Questions
Q1. Why does the Butterfly Pattern have two halves?
The upper half expands while the lower half contracts. Combining them creates the symmetric butterfly shape.
Q2. Why is the space formula 2 × (rows − row)?
Each row adds one star to both the left and right wings, reducing the center gap by two spaces. This keeps the butterfly symmetrical.
Q3. Why do we start the lower half from rows - 1?
The widest row has already been printed in the upper half. Starting from rows - 1 prevents the middle row from being printed twice.
Q4. Can the same logic be reused for other patterns?
Yes. The same approach can be adapted for:
- Hollow Butterfly
- Number Butterfly
- Alphabet Butterfly
- Mirror Butterfly
- Hollow Diamond
- Hourglass Pattern
Only the printed character or boundary condition changes.
Q5. How is the Butterfly Pattern different from the Diamond Pattern?
| Butterfly Pattern | Diamond Pattern |
|---|---|
| Two mirrored wings separated by spaces. | A single centered shape. |
| Stars grow from both sides. | Stars grow from the center. |
| Requires managing left wing, gap, and right wing. | Requires centered expansion and contraction. |
| Ideal for learning symmetry. | Ideal for learning centered patterns. |
Interview Tip
If an interviewer asks:
"Print a Butterfly Pattern in Java."
Break the problem into two independent halves:
-
Upper Half
- Print left stars.
- Print the center gap using
2 × (rows − row)spaces. - Print right stars.
-
Lower Half
- Repeat the same logic in reverse order, starting from
rows - 1.
- Repeat the same logic in reverse order, starting from
Explain that this decomposition simplifies the implementation and highlights the symmetry of the pattern. Once the basic butterfly is complete, mention that the same loop structure can easily be extended to create Hollow Butterfly, Number Butterfly, and Alphabet Butterfly patterns, demonstrating strong mastery of nested loops and pattern programming.