Apply Now Apply Now Apply Now
header_logo
Post thumbnail
JAVA

Master These 15 Star Patterns in Java to Ace Your Interview

By Lukesh S

Ever walked into a Java interview and blanked on a pattern question you’d seen a dozen times before? You’re not alone. Star patterns in Java are console-output exercises that use nested for loops to print asterisks (*) in shapes, triangles, pyramids, diamonds, hourglasses, and more. They seem simple, but that’s exactly why interviewers love them.

Companies like TCS, Infosys, Wipro, Cognizant, and Accenture use pattern questions in screening rounds because they test three things quickly: whether you understand nested loops, whether you can trace logic manually before writing code, and whether you can explain your thought process clearly. 

This article walks you through 15 star patterns, with full Java code, inline comments, expected output, and the concept each one tests. Whether you’re prepping for your first interview or brushing up, you’ll leave here ready.

Quick Answer: 

The 15 star patterns covered: Right Triangle, Inverted Right Triangle, Left Triangle, Pyramid, Inverted Pyramid, Full Pyramid (Diamond-body), Right Pascal’s Triangle, Hollow Pyramid, Diamond, Hollow Diamond, Sandglass, Hourglass, Butterfly, Hollow Butterfly, Hollow Square.

Table of contents


  1. Quick Reference: All 15 Star Patterns at a Glance
  2. How to Approach Any Star Patterns Problem
  3. The 15 Star Patterns Programs
    • Right Triangle Star Patterns
    • Inverted Right Triangle Star Patterns
    • Left Triangle Star Patterns
    • Pyramid Star Patterns
    • Inverted Pyramid Star Patterns
    • Full Pyramid Star Patterns(Upward + Downward Combined)
    • Right Pascal's Triangle Star Patterns
    • Hollow Pyramid Star Patterns
    • Diamond Star Patterns
    • Hollow Diamond Star Patterns
    • Sandglass Star Patterns
    • Hourglass Star Patterns
    • Butterfly Star Patterns
    • Hollow Butterfly Patterns
    • Hollow Square Patterns
  4. Common Mistakes to Avoid in Interviews
  5. Conclusion
  6. FAQs
    • What is a star pattern program in Java? 
    • Why are pattern programs asked in Java interviews? 
    • What is the most commonly asked star pattern in interviews? 
    • What is the difference between a solid and hollow star pattern? 
    • Can I use while loops instead of for loops for star patterns? 

Quick Reference: All 15 Star Patterns at a Glance

#Pattern NameDifficultyConcept TestedAsked At
1Right TriangleEasyBasic nested loopInfosys, Wipro
2Inverted Right TriangleEasyDecrementing outer loopWipro, HCL
3Left TriangleEasySpace + star alignmentTCS, Accenture
4PyramidEasySpaces + increasing starsTCS, Accenture
5Inverted PyramidEasySpaces + decreasing starsCognizant
6Full PyramidMediumCombining two pyramidsInfosys
7Right Pascal’s TriangleMediumUpper + inverted joinWipro, HCL
8Hollow PyramidMediumConditional border printingTCS
9DiamondMediumPyramid + inverted pyramidAll companies
10Hollow DiamondMediumBorder-only conditionTCS, Cognizant
11SandglassMediumInverted + upright pyramidHCL
12HourglassMediumSymmetric contractionWipro
13ButterflyHardMirror + space logicCognizant
14Hollow ButterflyHardBorder-only mirrorAccenture
15Hollow SquareMediumBorder condition on 2D gridWipro, HCL
All 15 Star Patterns at a Glance

How to Approach Any Star Patterns Problem

Every star pattern in Java, no matter how complex, breaks down the same way. You don’t need to memorise every pattern, you need to understand this mental framework:

  1. Count the rows — the outer loop always runs n times (user input or fixed)
  2. Identify what each row contains — spaces? stars? both?
  3. Write one inner loop per element type — one for spaces, one for stars
  4. Add if conditions only for hollow patterns — print * on borders, space inside
  5. Test manually with n = 4 — trace on paper before running

Once you have this framework, any new pattern becomes a variation you can figure out on the spot.

The 15 Star Patterns Programs

1. Right Triangle Star Patterns

A great starting point, the most fundamental nested loop pattern. The outer loop counts rows, the inner loop prints one star per column up to the row number.

import java.util.Scanner;

public class RightTriangleStarPattern {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter number of rows: ");

        int rows = scanner.nextInt();

        for (int i = 1; i <= rows; i++) {          // outer loop: row number

            for (int j = 1; j <= i; j++) {         // inner loop: stars = row number

                System.out.print("*");

            }

            System.out.println();                  // move to next line

        }

    }

}

```

**Output (n=5):**

```

*

**

***

****

*****

Concept tested: Basic nested loop, incrementing inner loop

MDN

2. Inverted Right Triangle Star Patterns

Flip the logic, start from the full row and count down. Just change the outer loop to decrement from rows to 1.

for (int i = rows; i >= 1; i--) {   // outer loop counts down

    for (int j = 1; j <= i; j++) {  // stars = current row count

        System.out.print("*");

    }

    System.out.println();

}

```

**Output (n=5):**

```

*****

****

***

**

*

Concept tested: Decrementing outer loop

3. Left Triangle Star Patterns

Same star count as the right triangle, but now you print spaces before the stars to push them right-aligned.

for (int i = 1; i <= rows; i++) {

    for (int j = 1; j <= rows - i; j++) {  // spaces for alignment

        System.out.print(" ");

    }

    for (int j = 1; j <= i; j++) {         // stars

        System.out.print("*");

    }

    System.out.println();

}

```

**Output (n=4):**

```

   *

  **

 ***

****

Concept tested: Two inner loops, space management

4. Pyramid Star Patterns

The classic. You need spaces on the left for centering, and an odd number of stars per row (2i – 1). Interviewers love asking this one because the space logic trips up many candidates.

for (int i = 1; i <= rows; i++) {

    for (int j = 1; j <= rows - i; j++) {    // leading spaces

        System.out.print(" ");

    }

    for (int j = 1; j <= 2 * i - 1; j++) {  // stars: 1, 3, 5, 7...

        System.out.print("*");

    }

    System.out.println();

}

```

**Output (n=4):**

```

   *

  ***

 *****

*******

Concept tested: Space + star calculation, 2i – 1 formula

💡 Did You Know?

The formula 2i – 1 always gives odd numbers, that’s why a pyramid always has a clean point at the top. Row 1 = 1 star, row 2 = 3 stars, row 3 = 5 stars. Spot the pattern? It’s an arithmetic sequence with common difference 2.

5. Inverted Pyramid Star Patterns

Start with the widest row at the top, then shrink it. Space count increases as stars decrease.

for (int i = rows; i >= 1; i--) {

    for (int j = 1; j <= rows - i; j++) {    // increasing spaces

        System.out.print(" ");

    }

    for (int j = 1; j <= 2 * i - 1; j++) {  // decreasing stars

        System.out.print("*");

    }

    System.out.println();

}

```

**Output (n=4):**

```

*******

 *****

  ***

   *

Concept tested: Inverse relationship between spaces and stars

6. Full Pyramid Star Patterns(Upward + Downward Combined)

You join a pyramid and an inverted pyramid to form a rhombus shape. Two separate loop blocks, one going up, one going down.

// Upper pyramid

for (int i = 1; i <= rows; i++) {

    for (int j = 1; j <= rows - i; j++) System.out.print(" ");

    for (int j = 1; j <= 2 * i - 1; j++) System.out.print("*");

    System.out.println();

}

// Lower inverted pyramid (skip the middle row with rows-1)

for (int i = rows - 1; i >= 1; i--) {

    for (int j = 1; j <= rows - i; j++) System.out.print(" ");

    for (int j = 1; j <= 2 * i - 1; j++) System.out.print("*");

    System.out.println();

}

```

**Output (n=4):**

```

   *

  ***

 *****

*******

 *****

  ***

   *

Concept tested: Two-part pattern, mirroring logic

If you want to be more confident about Java and learn all its knacks, then consider enrolling for HCL GUVI’s Free Self-Paced Java Programming Course that covers everything from variables and control flow to OOP, at your own pace, in your language.

7. Right Pascal’s Triangle Star Patterns

An upper right triangle joined with a downward-pointing one. Think of it as a “mountain” lying on its side.

for (int i = 1; i <= rows; i++) {         // upper: increasing

    for (int j = 1; j <= i; j++) System.out.print("*");

    System.out.println();

}

for (int i = rows - 1; i >= 1; i--) {    // lower: decreasing

    for (int j = 1; j <= i; j++) System.out.print("*");

    System.out.println();

}

```

**Output (n=4):**

```

*

**

***

****

***

**

*

Concept tested: Combining ascending and descending triangles

8. Hollow Pyramid Star Patterns

Same outer structure as the pyramid, but only border stars are printed. An if condition gates the interior, only the first column, last column, and bottom row get a *.

for (int i = 1; i <= rows; i++) {

    for (int j = 1; j <= rows - i; j++) System.out.print(" ");

    for (int j = 1; j <= 2 * i - 1; j++) {

        if (j == 1 || j == 2 * i - 1 || i == rows) { // border condition

            System.out.print("*");

        } else {

            System.out.print(" ");

        }

    }

    System.out.println();

}

```

**Output (n=4):**

```

   *

  * *

 *   *

*******

Concept tested: Conditional printing, border vs interior distinction

9. Diamond Star Patterns

A diamond is a pyramid on top of an inverted pyramid, sharing no duplicate middle row. This is one of the most commonly asked medium-difficulty patterns.

// Upper half

for (int i = 1; i <= rows; i++) {

    for (int j = 1; j <= rows - i; j++) System.out.print(" ");

    for (int j = 1; j <= 2 * i - 1; j++) System.out.print("*");

    System.out.println();

}

// Lower half

for (int i = rows - 1; i >= 1; i--) {

    for (int j = 1; j <= rows - i; j++) System.out.print(" ");

    for (int j = 1; j <= 2 * i - 1; j++) System.out.print("*");

    System.out.println();

}

```

**Output (n=4):**

```

   *

  ***

 *****

*******

 *****

  ***

   *

Concept tested: Pyramid + inverted pyramid combination

10. Hollow Diamond Star Patterns

A hollow diamond prints only the outermost stars, spaces fill the inside. The condition checks if the star is on the first or last column of each row.

// Upper hollow pyramid

for (int i = 1; i <= rows; i++) {

    for (int j = 1; j <= rows - i; j++) System.out.print(" ");

    for (int j = 1; j <= 2 * i - 1; j++) {

        if (j == 1 || j == 2 * i - 1) System.out.print("*"); // only borders

        else System.out.print(" ");

    }

    System.out.println();

}

// Lower hollow inverted pyramid

for (int i = rows - 1; i >= 1; i--) {

    for (int j = 1; j <= rows - i; j++) System.out.print(" ");

    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 (n=4):**

```

   *

  * *

 *   *

*     *

 *   *

  * *

   *

Concept tested: Border-only condition on a symmetric shape

11. Sandglass Star Patterns

Wide at the top and bottom, narrow in the middle — like a sandglass. Start with the inverted pyramid, then build upward.

// Top: inverted pyramid
for (int i = rows; i >= 1; i--) {
    for (int j = 1; j <= rows - i; j++) System.out.print(" ");
    for (int j = 1; j <= 2 * i - 1; j++) System.out.print("*");
    System.out.println();
}
// Bottom: upright pyramid
for (int i = 2; i <= rows; i++) {
    for (int j = 1; j <= rows - i; j++) System.out.print(" ");
    for (int j = 1; j <= 2 * i - 1; j++) System.out.print("*");
    System.out.println();
}
```
**Output (n=4):**
```
*******
 *****
  ***
   *
  ***
 *****
*******

Concept tested: Inverted + upright pyramid join, loop starting point

12. Hourglass Star Patterns

Similar to sandglass but the widest rows are at the outer edges and the narrowest in the middle. Start from row rows counting down, then continue from row 2 counting up.

java

for (int i = rows; i >= 1; i--) {

    for (int j = 1; j <= rows - i; j++) System.out.print(" ");

    for (int j = 1; j <= 2 * i - 1; j++) System.out.print("*");

    System.out.println();

}

for (int i = 2; i <= rows; i++) {

    for (int j = 1; j <= rows - i; j++) System.out.print(" ");

    for (int j = 1; j <= 2 * i - 1; j++) System.out.print("*");

    System.out.println();

}

```

**Output (n=4):**

```

*******

 *****

  ***

   *

  ***

 *****

*******

Concept tested: Symmetric contraction and expansion

13. Butterfly Star Patterns

This one challenges you to print two mirrored right triangles on the same row, separated by a gap. The gap shrinks as rows increase.

// Upper half

for (int i = 1; i <= rows; i++) {

    for (int j = 1; j <= i; j++) System.out.print("*");          // left wing

    for (int j = 1; j <= 2 * (rows - i); j++) System.out.print(" "); // gap

    for (int j = 1; j <= i; j++) System.out.print("*");          // right wing

    System.out.println();

}

// Lower half

for (int i = rows; i >= 1; i--) {

    for (int j = 1; j <= i; j++) System.out.print("*");

    for (int j = 1; j <= 2 * (rows - i); j++) System.out.print(" ");

    for (int j = 1; j <= i; j++) System.out.print("*");

    System.out.println();

}

```

**Output (n=4):**

```

*      *

**    **

***  ***

********

***  ***

**    **

*      *

Concept tested: Three inner loops per row, mirror symmetry, gap formula

14. Hollow Butterfly Patterns

Same structure as the butterfly, but only border positions print a star. Add a border condition inside each wing.

for (int i = 1; i <= rows; i++) {

    for (int j = 1; j <= i; j++) {

        if (j == 1 || j == i) System.out.print("*"); else System.out.print(" ");

    }

    for (int j = 1; j <= 2 * (rows - i); j++) System.out.print(" ");

    for (int j = 1; j <= i; j++) {

        if (j == 1 || j == i) System.out.print("*"); else System.out.print(" ");

    }

    System.out.println();

}

for (int i = rows; i >= 1; i--) {

    for (int j = 1; j <= i; j++) {

        if (j == 1 || j == i) System.out.print("*"); else System.out.print(" ");

    }

    for (int j = 1; j <= 2 * (rows - i); j++) System.out.print(" ");

    for (int j = 1; j <= i; j++) {

        if (j == 1 || j == i) System.out.print("*"); else System.out.print(" ");

    }

    System.out.println();

}

```

**Output (n=4):**

```

*      *

**    **

* *  * *

*  **  *

*  **  *

* *  * *

**    **

*      *

Concept tested: Border conditions on mirrored wings

15. Hollow Square Patterns

A 2D grid where only the border cells print a star. The condition is simple, first row, last row, first column, or last column.

for (int i = 1; i <= rows; i++) {

    for (int j = 1; j <= rows; j++) {

        if (i == 1 || i == rows || j == 1 || j == rows) // border check

            System.out.print("*");

        else

            System.out.print(" ");

    }

    System.out.println();

}

```

**Output (n=5):**

```

*****

*   *

*   *

*   *

*****

Concept tested: 2D grid traversal, multi-condition border check

Common Mistakes to Avoid in Interviews

Even if you know the code, these slip-ups can cost you:

  • Off-by-one errors — check whether your loop starts at 0 or 1 and whether it ends at < n or <= n
  • Forgetting System.out.println() — missing the newline after each row collapses your pattern into one line
  • Wrong space formula — for pyramids, spaces = rows - i, not i - 1 (they give different results for off-center alignment)
  • Explaining nothing — writing silent code in an interview is a red flag; narrate your loop logic as you write it
  • Hardcoding n — always take n as user input via Scanner unless told otherwise

Master Star Patterns in Java Full Stack Development with HCL GUVI’s industry-relevant Java Full Stack Development course designed by IIT-M & IIM experts. Build real-world projects, get hands-on with tools like Spring, Hibernate, and React, and prepare confidently for top-tier Java Full Stack Developer interviews.

Conclusion

In conclusion, star patterns programs aren't just interview filler, they're a genuine test of how you think through a problem step by step. If you can look at a diamond or a butterfly and immediately start breaking it into rows, loops, and conditions, you've built the kind of logical thinking that carries over into algorithms, data structures, and real-world Java development.

The 15 star patterns in this guide cover everything from basic triangles to complex hollow butterflies. Work through them in order, test each one with n = 4 and n = 5, and try explaining your logic out loud, that last part is what interviewers actually remember.

FAQs

1. What is a star pattern program in Java? 

A star pattern program in Java is a program that uses nested for loops to print asterisks (*) in geometric shapes on the console, such as triangles, pyramids, diamonds, and squares. They test loop logic and are standard in Java screening interviews.

2. Why are pattern programs asked in Java interviews? 

Interviewers use them to test nested loop understanding, manual logic tracing, and the ability to explain code clearly, all in one compact question. They're time-efficient tests of fundamental programming skill.

3. What is the most commonly asked star pattern in interviews? 

The pyramid pattern (2i - 1 stars per row) is the most frequently asked, followed closely by the diamond and the right triangle. If you're short on time, master these three first.

4. What is the difference between a solid and hollow star pattern? 

In a solid pattern, every cell within the shape boundary prints a *. In a hollow pattern, only the border cells print *, interior cells print a space. A single if condition is all that separates them.

MDN

5. Can I use while loops instead of for loops for star patterns? 

Yes, any for loop can be rewritten as a while loop. However, for loops are preferred in interviews because they're more compact and clearly express the start, condition, and increment in one line.

Success Stories

Did you enjoy this article?

Schedule 1:1 free counselling

Similar Articles

Loading...
Get in Touch
Chat on Whatsapp
Request Callback
Share logo Copy link
Table of contents Table of contents
Table of contents Articles
Close button

  1. Quick Reference: All 15 Star Patterns at a Glance
  2. How to Approach Any Star Patterns Problem
  3. The 15 Star Patterns Programs
    • Right Triangle Star Patterns
    • Inverted Right Triangle Star Patterns
    • Left Triangle Star Patterns
    • Pyramid Star Patterns
    • Inverted Pyramid Star Patterns
    • Full Pyramid Star Patterns(Upward + Downward Combined)
    • Right Pascal's Triangle Star Patterns
    • Hollow Pyramid Star Patterns
    • Diamond Star Patterns
    • Hollow Diamond Star Patterns
    • Sandglass Star Patterns
    • Hourglass Star Patterns
    • Butterfly Star Patterns
    • Hollow Butterfly Patterns
    • Hollow Square Patterns
  4. Common Mistakes to Avoid in Interviews
  5. Conclusion
  6. FAQs
    • What is a star pattern program in Java? 
    • Why are pattern programs asked in Java interviews? 
    • What is the most commonly asked star pattern in interviews? 
    • What is the difference between a solid and hollow star pattern? 
    • Can I use while loops instead of for loops for star patterns?