{"id":71105,"date":"2025-01-30T15:33:04","date_gmt":"2025-01-30T10:03:04","guid":{"rendered":"https:\/\/www.guvi.in\/blog\/?p=71105"},"modified":"2026-04-01T10:48:51","modified_gmt":"2026-04-01T05:18:51","slug":"star-patterns-in-java-for-your-next-interview","status":"publish","type":"post","link":"https:\/\/www.guvi.in\/blog\/star-patterns-in-java-for-your-next-interview\/","title":{"rendered":"Master These 15 Star Patterns in Java to Ace Your Interview"},"content":{"rendered":"\n<p>Ever walked into a Java interview and blanked on a pattern question you&#8217;d seen a dozen times before? You&#8217;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&#8217;s exactly why interviewers love them.<\/p>\n\n\n\n<p>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.&nbsp;<\/p>\n\n\n\n<p>This article walks you through 15 star patterns, with full Java code, inline comments, expected output, and the concept each one tests. Whether you&#8217;re prepping for your first interview or brushing up, you&#8217;ll leave here ready.<\/p>\n\n\n\n<p><strong>Quick Answer:&nbsp;<\/strong><\/p>\n\n\n\n<p>The 15 star patterns covered: Right Triangle, Inverted Right Triangle, Left Triangle, Pyramid, Inverted Pyramid, Full Pyramid (Diamond-body), Right Pascal&#8217;s Triangle, Hollow Pyramid, Diamond, Hollow Diamond, Sandglass, Hourglass, Butterfly, Hollow Butterfly, Hollow Square.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Quick Reference: All 15 Star Patterns at a Glance<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>#<\/strong><\/td><td><strong>Pattern Name<\/strong><\/td><td><strong>Difficulty<\/strong><\/td><td><strong>Concept Tested<\/strong><\/td><td><strong>Asked At<\/strong><\/td><\/tr><tr><td>1<\/td><td>Right Triangle<\/td><td>Easy<\/td><td>Basic nested loop<\/td><td>Infosys, Wipro<\/td><\/tr><tr><td>2<\/td><td>Inverted Right Triangle<\/td><td>Easy<\/td><td>Decrementing outer loop<\/td><td>Wipro, HCL<\/td><\/tr><tr><td>3<\/td><td>Left Triangle<\/td><td>Easy<\/td><td>Space + star alignment<\/td><td>TCS, Accenture<\/td><\/tr><tr><td>4<\/td><td>Pyramid<\/td><td>Easy<\/td><td>Spaces + increasing stars<\/td><td>TCS, Accenture<\/td><\/tr><tr><td>5<\/td><td>Inverted Pyramid<\/td><td>Easy<\/td><td>Spaces + decreasing stars<\/td><td>Cognizant<\/td><\/tr><tr><td>6<\/td><td>Full Pyramid<\/td><td>Medium<\/td><td>Combining two pyramids<\/td><td>Infosys<\/td><\/tr><tr><td>7<\/td><td>Right Pascal&#8217;s Triangle<\/td><td>Medium<\/td><td>Upper + inverted join<\/td><td>Wipro, HCL<\/td><\/tr><tr><td>8<\/td><td>Hollow Pyramid<\/td><td>Medium<\/td><td>Conditional border printing<\/td><td>TCS<\/td><\/tr><tr><td>9<\/td><td>Diamond<\/td><td>Medium<\/td><td>Pyramid + inverted pyramid<\/td><td>All companies<\/td><\/tr><tr><td>10<\/td><td>Hollow Diamond<\/td><td>Medium<\/td><td>Border-only condition<\/td><td>TCS, Cognizant<\/td><\/tr><tr><td>11<\/td><td>Sandglass<\/td><td>Medium<\/td><td>Inverted + upright pyramid<\/td><td>HCL<\/td><\/tr><tr><td>12<\/td><td>Hourglass<\/td><td>Medium<\/td><td>Symmetric contraction<\/td><td>Wipro<\/td><\/tr><tr><td>13<\/td><td>Butterfly<\/td><td>Hard<\/td><td>Mirror + space logic<\/td><td>Cognizant<\/td><\/tr><tr><td>14<\/td><td>Hollow Butterfly<\/td><td>Hard<\/td><td>Border-only mirror<\/td><td>Accenture<\/td><\/tr><tr><td>15<\/td><td>Hollow Square<\/td><td>Medium<\/td><td>Border condition on 2D grid<\/td><td>Wipro, HCL<\/td><\/tr><\/tbody><\/table><figcaption class=\"wp-element-caption\"><strong>All 15 Star Patterns at a Glance<\/strong><\/figcaption><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>How to Approach Any Star Patterns Problem<\/strong><\/h2>\n\n\n\n<p>Every star pattern in <a href=\"https:\/\/www.guvi.in\/blog\/introduction-to-java\/\" target=\"_blank\" rel=\"noreferrer noopener\">Java<\/a>, no matter how complex, breaks down the same way. You don&#8217;t need to memorise every pattern, you need to understand this mental framework:<\/p>\n\n\n\n<ol>\n<li><strong>Count the rows<\/strong> \u2014 the outer loop always runs n times (user input or fixed)<\/li>\n\n\n\n<li><strong>Identify what each row contains<\/strong> \u2014 spaces? stars? both?<\/li>\n\n\n\n<li><strong>Write one inner loop per element type<\/strong> \u2014 one for spaces, one for stars<\/li>\n\n\n\n<li><strong>Add <\/strong><a href=\"https:\/\/www.guvi.in\/hub\/java-programs-tutorial\/if-else-program\/\" target=\"_blank\" rel=\"noreferrer noopener\"><strong>if conditions<\/strong><\/a><strong> only for hollow patterns<\/strong> \u2014 print * on borders, space inside<\/li>\n\n\n\n<li><strong>Test manually with n = 4<\/strong> \u2014 trace on paper before running<\/li>\n<\/ol>\n\n\n\n<p>Once you have this framework, any new pattern becomes a variation you can figure out on the spot.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>The 15 Star Patterns Programs<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>1. Right Triangle Star Patterns<\/strong><\/h3>\n\n\n\n<p>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.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.util.Scanner;\n\npublic class RightTriangleStarPattern {\n\n&nbsp;&nbsp;&nbsp;&nbsp;public static void main(String&#91;] args) {\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Scanner scanner = new Scanner(System.in);\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.print(\"Enter number of rows: \");\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int rows = scanner.nextInt();\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for (int i = 1; i &lt;= rows; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \/\/ outer loop: row number\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for (int j = 1; j &lt;= i; j++) { &nbsp; &nbsp; &nbsp; &nbsp; \/\/ inner loop: stars = row number\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.print(\"*\");\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \/\/ move to next line\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}\n\n&nbsp;&nbsp;&nbsp;&nbsp;}\n\n}\n\n```\n\n**Output (n=5):**\n\n```\n\n*\n\n**\n\n***\n\n****\n\n*****<\/code><\/pre>\n\n\n\n<p><strong>Concept tested:<\/strong> Basic nested loop, incrementing inner loop<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2. Inverted Right Triangle Star Patterns<\/strong><\/h3>\n\n\n\n<p>Flip the logic, start from the full row and count down. Just change the outer loop to decrement from rows to 1.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>for (int i = rows; i &gt;= 1; i--) { &nbsp; \/\/ outer loop counts down\n\n&nbsp;&nbsp;&nbsp;&nbsp;for (int j = 1; j &lt;= i; j++) {&nbsp; \/\/ stars = current row count\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.print(\"*\");\n\n&nbsp;&nbsp;&nbsp;&nbsp;}\n\n&nbsp;&nbsp;&nbsp;&nbsp;System.out.println();\n\n}\n\n```\n\n**Output (n=5):**\n\n```\n\n*****\n\n****\n\n***\n\n**\n\n*<\/code><\/pre>\n\n\n\n<p><strong>Concept tested:<\/strong> Decrementing outer loop<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3. Left Triangle Star Patterns<\/strong><\/h3>\n\n\n\n<p>Same star count as the right triangle, but now you print spaces before the stars to push them right-aligned.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>for (int i = 1; i &lt;= rows; i++) {\n\n&nbsp;&nbsp;&nbsp;&nbsp;for (int j = 1; j &lt;= rows - i; j++) {&nbsp; \/\/ spaces for alignment\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.print(\" \");\n\n&nbsp;&nbsp;&nbsp;&nbsp;}\n\n&nbsp;&nbsp;&nbsp;&nbsp;for (int j = 1; j &lt;= i; j++) { &nbsp; &nbsp; &nbsp; &nbsp; \/\/ stars\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.print(\"*\");\n\n&nbsp;&nbsp;&nbsp;&nbsp;}\n\n&nbsp;&nbsp;&nbsp;&nbsp;System.out.println();\n\n}\n\n```\n\n**Output (n=4):**\n\n```\n\n&nbsp;&nbsp;&nbsp;*\n\n&nbsp;&nbsp;**\n\n&nbsp;***\n\n****<\/code><\/pre>\n\n\n\n<p><strong>Concept tested:<\/strong> Two inner loops, space management<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>4. Pyramid Star Patterns<\/strong><\/h3>\n\n\n\n<p>The classic. You need spaces on the left for centering, and an odd number of stars per row (2i &#8211; 1). Interviewers love asking this one because the space logic trips up many candidates.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>for (int i = 1; i &lt;= rows; i++) {\n\n&nbsp;&nbsp;&nbsp;&nbsp;for (int j = 1; j &lt;= rows - i; j++) {&nbsp; &nbsp; \/\/ leading spaces\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.print(\" \");\n\n&nbsp;&nbsp;&nbsp;&nbsp;}\n\n&nbsp;&nbsp;&nbsp;&nbsp;for (int j = 1; j &lt;= 2 * i - 1; j++) {&nbsp; \/\/ stars: 1, 3, 5, 7...\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.print(\"*\");\n\n&nbsp;&nbsp;&nbsp;&nbsp;}\n\n&nbsp;&nbsp;&nbsp;&nbsp;System.out.println();\n\n}\n\n```\n\n**Output (n=4):**\n\n```\n\n&nbsp;&nbsp;&nbsp;*\n\n&nbsp;&nbsp;***\n\n&nbsp;*****\n\n*******<\/code><\/pre>\n\n\n\n<p><strong>Concept tested:<\/strong> Space + star calculation, 2i &#8211; 1 formula<\/p>\n\n\n\n<div style=\"background-color: #099f4e; border: 3px solid #110053; border-radius: 12px; padding: 18px 22px; color: #FFFFFF; font-size: 18px; font-family: Montserrat, Helvetica, sans-serif; line-height: 1.6; box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15); max-width: 750px;\"><strong style=\"font-size: 22px; color: #FFFFFF;\">\ud83d\udca1 Did You Know?<\/strong> <br \/><br \/>The formula 2i &#8211; 1 always gives odd numbers, that&#8217;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&#8217;s an arithmetic sequence with common difference 2.<\/div>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>5. Inverted Pyramid Star Patterns<\/strong><\/h3>\n\n\n\n<p>Start with the widest row at the top, then shrink it. Space count increases as stars decrease.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>for (int i = rows; i &gt;= 1; i--) {\n\n&nbsp;&nbsp;&nbsp;&nbsp;for (int j = 1; j &lt;= rows - i; j++) {&nbsp; &nbsp; \/\/ increasing spaces\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.print(\" \");\n\n&nbsp;&nbsp;&nbsp;&nbsp;}\n\n&nbsp;&nbsp;&nbsp;&nbsp;for (int j = 1; j &lt;= 2 * i - 1; j++) {&nbsp; \/\/ decreasing stars\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.print(\"*\");\n\n&nbsp;&nbsp;&nbsp;&nbsp;}\n\n&nbsp;&nbsp;&nbsp;&nbsp;System.out.println();\n\n}\n\n```\n\n**Output (n=4):**\n\n```\n\n*******\n\n&nbsp;*****\n\n&nbsp;&nbsp;***\n\n&nbsp;&nbsp;&nbsp;*<\/code><\/pre>\n\n\n\n<p><strong>Concept tested:<\/strong> Inverse relationship between spaces and stars<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>6. Full Pyramid Star Patterns(Upward + Downward Combined)<\/strong><\/h3>\n\n\n\n<p>You join a pyramid and an inverted pyramid to form a rhombus shape. Two separate loop blocks, one going up, one going down.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ Upper pyramid\n\nfor (int i = 1; i &lt;= rows; i++) {\n\n&nbsp;&nbsp;&nbsp;&nbsp;for (int j = 1; j &lt;= rows - i; j++) System.out.print(\" \");\n\n&nbsp;&nbsp;&nbsp;&nbsp;for (int j = 1; j &lt;= 2 * i - 1; j++) System.out.print(\"*\");\n\n&nbsp;&nbsp;&nbsp;&nbsp;System.out.println();\n\n}\n\n\/\/ Lower inverted pyramid (skip the middle row with rows-1)\n\nfor (int i = rows - 1; i &gt;= 1; i--) {\n\n&nbsp;&nbsp;&nbsp;&nbsp;for (int j = 1; j &lt;= rows - i; j++) System.out.print(\" \");\n\n&nbsp;&nbsp;&nbsp;&nbsp;for (int j = 1; j &lt;= 2 * i - 1; j++) System.out.print(\"*\");\n\n&nbsp;&nbsp;&nbsp;&nbsp;System.out.println();\n\n}\n\n```\n\n**Output (n=4):**\n\n```\n\n&nbsp;&nbsp;&nbsp;*\n\n&nbsp;&nbsp;***\n\n&nbsp;*****\n\n*******\n\n&nbsp;*****\n\n&nbsp;&nbsp;***\n\n&nbsp;&nbsp;&nbsp;*<\/code><\/pre>\n\n\n\n<p><strong>Concept tested:<\/strong> Two-part pattern, mirroring logic<\/p>\n\n\n\n<p>If you want to be more confident about Java and learn all its knacks, then consider enrolling for HCL GUVI&#8217;s<a href=\"https:\/\/www.guvi.in\/courses\/programming\/java-programming\/?utm_source=blog&amp;utm_medium=hyperlink+&amp;utm_campaign=star-patterns-in-java-for-your-next-interview\" target=\"_blank\" rel=\"noreferrer noopener\"> <\/a>Free Self-Paced<a href=\"https:\/\/www.guvi.in\/courses\/programming\/java-programming\/?utm_source=blog&amp;utm_medium=hyperlink+&amp;utm_campaign=star-patterns-in-java-for-your-next-interview\" target=\"_blank\" rel=\"noreferrer noopener\"> Java Programming Course<\/a> that covers everything from variables and control flow to OOP, at your own pace, in your language.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>7. Right Pascal&#8217;s Triangle<\/strong> <strong>Star Patterns<\/strong><\/h3>\n\n\n\n<p>An upper right triangle joined with a downward-pointing one. Think of it as a &#8220;mountain&#8221; lying on its side.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>for (int i = 1; i &lt;= rows; i++) { &nbsp; &nbsp; &nbsp; &nbsp; \/\/ upper: increasing\n\n&nbsp;&nbsp;&nbsp;&nbsp;for (int j = 1; j &lt;= i; j++) System.out.print(\"*\");\n\n&nbsp;&nbsp;&nbsp;&nbsp;System.out.println();\n\n}\n\nfor (int i = rows - 1; i &gt;= 1; i--) {&nbsp; &nbsp; \/\/ lower: decreasing\n\n&nbsp;&nbsp;&nbsp;&nbsp;for (int j = 1; j &lt;= i; j++) System.out.print(\"*\");\n\n&nbsp;&nbsp;&nbsp;&nbsp;System.out.println();\n\n}\n\n```\n\n**Output (n=4):**\n\n```\n\n*\n\n**\n\n***\n\n****\n\n***\n\n**\n\n*<\/code><\/pre>\n\n\n\n<p><strong>Concept tested:<\/strong> Combining ascending and descending triangles<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>8. Hollow Pyramid Star Patterns<\/strong><\/h3>\n\n\n\n<p>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 *.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>for (int i = 1; i &lt;= rows; i++) {\n\n&nbsp;&nbsp;&nbsp;&nbsp;for (int j = 1; j &lt;= rows - i; j++) System.out.print(\" \");\n\n&nbsp;&nbsp;&nbsp;&nbsp;for (int j = 1; j &lt;= 2 * i - 1; j++) {\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (j == 1 || j == 2 * i - 1 || i == rows) { \/\/ border condition\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.print(\"*\");\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} else {\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.print(\" \");\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}\n\n&nbsp;&nbsp;&nbsp;&nbsp;}\n\n&nbsp;&nbsp;&nbsp;&nbsp;System.out.println();\n\n}\n\n```\n\n**Output (n=4):**\n\n```\n\n&nbsp;&nbsp;&nbsp;*\n\n&nbsp;&nbsp;* *\n\n&nbsp;* &nbsp; *\n\n*******<\/code><\/pre>\n\n\n\n<p><strong>Concept tested:<\/strong> Conditional printing, border vs interior distinction<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>9. Diamond Star Patterns<\/strong><\/h3>\n\n\n\n<p>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.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ Upper half\n\nfor (int i = 1; i &lt;= rows; i++) {\n\n&nbsp;&nbsp;&nbsp;&nbsp;for (int j = 1; j &lt;= rows - i; j++) System.out.print(\" \");\n\n&nbsp;&nbsp;&nbsp;&nbsp;for (int j = 1; j &lt;= 2 * i - 1; j++) System.out.print(\"*\");\n\n&nbsp;&nbsp;&nbsp;&nbsp;System.out.println();\n\n}\n\n\/\/ Lower half\n\nfor (int i = rows - 1; i &gt;= 1; i--) {\n\n&nbsp;&nbsp;&nbsp;&nbsp;for (int j = 1; j &lt;= rows - i; j++) System.out.print(\" \");\n\n&nbsp;&nbsp;&nbsp;&nbsp;for (int j = 1; j &lt;= 2 * i - 1; j++) System.out.print(\"*\");\n\n&nbsp;&nbsp;&nbsp;&nbsp;System.out.println();\n\n}\n\n```\n\n**Output (n=4):**\n\n```\n\n&nbsp;&nbsp;&nbsp;*\n\n&nbsp;&nbsp;***\n\n&nbsp;*****\n\n*******\n\n&nbsp;*****\n\n&nbsp;&nbsp;***\n\n&nbsp;&nbsp;&nbsp;*<\/code><\/pre>\n\n\n\n<p><strong>Concept tested:<\/strong> Pyramid + inverted pyramid combination<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>10. Hollow Diamond Star Patterns<\/strong><\/h3>\n\n\n\n<p>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.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ Upper hollow pyramid\n\nfor (int i = 1; i &lt;= rows; i++) {\n\n&nbsp;&nbsp;&nbsp;&nbsp;for (int j = 1; j &lt;= rows - i; j++) System.out.print(\" \");\n\n&nbsp;&nbsp;&nbsp;&nbsp;for (int j = 1; j &lt;= 2 * i - 1; j++) {\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (j == 1 || j == 2 * i - 1) System.out.print(\"*\"); \/\/ only borders\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else System.out.print(\" \");\n\n&nbsp;&nbsp;&nbsp;&nbsp;}\n\n&nbsp;&nbsp;&nbsp;&nbsp;System.out.println();\n\n}\n\n\/\/ Lower hollow inverted pyramid\n\nfor (int i = rows - 1; i &gt;= 1; i--) {\n\n&nbsp;&nbsp;&nbsp;&nbsp;for (int j = 1; j &lt;= rows - i; j++) System.out.print(\" \");\n\n&nbsp;&nbsp;&nbsp;&nbsp;for (int j = 1; j &lt;= 2 * i - 1; j++) {\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (j == 1 || j == 2 * i - 1) System.out.print(\"*\");\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else System.out.print(\" \");\n\n&nbsp;&nbsp;&nbsp;&nbsp;}\n\n&nbsp;&nbsp;&nbsp;&nbsp;System.out.println();\n\n}\n\n```\n\n**Output (n=4):**\n\n```\n\n&nbsp;&nbsp;&nbsp;*\n\n&nbsp;&nbsp;* *\n\n&nbsp;* &nbsp; *\n\n* &nbsp; &nbsp; *\n\n&nbsp;* &nbsp; *\n\n&nbsp;&nbsp;* *\n\n&nbsp;&nbsp;&nbsp;*<\/code><\/pre>\n\n\n\n<p><strong>Concept tested:<\/strong> Border-only condition on a symmetric shape<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>11. Sandglass Star Patterns<\/strong><\/h3>\n\n\n\n<p>Wide at the top and bottom, narrow in the middle \u2014 like a sandglass. Start with the inverted pyramid, then build upward.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ Top: inverted pyramid\nfor (int i = rows; i >= 1; i--) {\n\u00a0\u00a0\u00a0\u00a0for (int j = 1; j <= rows - i; j++) System.out.print(\" \");\n\u00a0\u00a0\u00a0\u00a0for (int j = 1; j <= 2 * i - 1; j++) System.out.print(\"*\");\n\u00a0\u00a0\u00a0\u00a0System.out.println();\n}\n\/\/ Bottom: upright pyramid\nfor (int i = 2; i <= rows; i++) {\n\u00a0\u00a0\u00a0\u00a0for (int j = 1; j <= rows - i; j++) System.out.print(\" \");\n\u00a0\u00a0\u00a0\u00a0for (int j = 1; j <= 2 * i - 1; j++) System.out.print(\"*\");\n\u00a0\u00a0\u00a0\u00a0System.out.println();\n}\n```\n**Output (n=4):**\n```\n*******\n\u00a0*****\n\u00a0\u00a0***\n\u00a0\u00a0\u00a0*\n\u00a0\u00a0***\n\u00a0*****\n*******<\/code><\/pre>\n\n\n\n<p><strong>Concept tested:<\/strong> Inverted + upright pyramid join, loop starting point<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>12. Hourglass Star Patterns<\/strong><\/h3>\n\n\n\n<p>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.<\/p>\n\n\n\n<p>java<\/p>\n\n\n\n<p>for (int i = rows; i &gt;= 1; i--) {<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;for (int j = 1; j &lt;= rows - i; j++) System.out.print(\" \");<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;for (int j = 1; j &lt;= 2 * i - 1; j++) System.out.print(\"*\");<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;System.out.println();<\/p>\n\n\n\n<p>}<\/p>\n\n\n\n<p>for (int i = 2; i &lt;= rows; i++) {<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;for (int j = 1; j &lt;= rows - i; j++) System.out.print(\" \");<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;for (int j = 1; j &lt;= 2 * i - 1; j++) System.out.print(\"*\");<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;System.out.println();<\/p>\n\n\n\n<p>}<\/p>\n\n\n\n<p>```<\/p>\n\n\n\n<p>**Output (n=4):**<\/p>\n\n\n\n<p>```<\/p>\n\n\n\n<p>*******<\/p>\n\n\n\n<p>&nbsp;*****<\/p>\n\n\n\n<p>&nbsp;&nbsp;***<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;*<\/p>\n\n\n\n<p>&nbsp;&nbsp;***<\/p>\n\n\n\n<p>&nbsp;*****<\/p>\n\n\n\n<p>*******<\/p>\n\n\n\n<p><strong>Concept tested:<\/strong> Symmetric contraction and expansion<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>13. Butterfly Star Patterns<\/strong><\/h3>\n\n\n\n<p>This one challenges you to print two mirrored right triangles on the same row, separated by a gap. The gap shrinks as rows increase.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ Upper half\n\nfor (int i = 1; i &lt;= rows; i++) {\n\n&nbsp;&nbsp;&nbsp;&nbsp;for (int j = 1; j &lt;= i; j++) System.out.print(\"*\");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \/\/ left wing\n\n&nbsp;&nbsp;&nbsp;&nbsp;for (int j = 1; j &lt;= 2 * (rows - i); j++) System.out.print(\" \"); \/\/ gap\n\n&nbsp;&nbsp;&nbsp;&nbsp;for (int j = 1; j &lt;= i; j++) System.out.print(\"*\");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \/\/ right wing\n\n&nbsp;&nbsp;&nbsp;&nbsp;System.out.println();\n\n}\n\n\/\/ Lower half\n\nfor (int i = rows; i &gt;= 1; i--) {\n\n&nbsp;&nbsp;&nbsp;&nbsp;for (int j = 1; j &lt;= i; j++) System.out.print(\"*\");\n\n&nbsp;&nbsp;&nbsp;&nbsp;for (int j = 1; j &lt;= 2 * (rows - i); j++) System.out.print(\" \");\n\n&nbsp;&nbsp;&nbsp;&nbsp;for (int j = 1; j &lt;= i; j++) System.out.print(\"*\");\n\n&nbsp;&nbsp;&nbsp;&nbsp;System.out.println();\n\n}\n\n```\n\n**Output (n=4):**\n\n```\n\n*&nbsp; &nbsp; &nbsp; *\n\n**&nbsp; &nbsp; **\n\n***&nbsp; ***\n\n********\n\n***&nbsp; ***\n\n**&nbsp; &nbsp; **\n\n*&nbsp; &nbsp; &nbsp; *<\/code><\/pre>\n\n\n\n<p><strong>Concept tested:<\/strong> Three inner loops per row, mirror symmetry, gap formula<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>14. Hollow Butterfly Patterns<\/strong><\/h3>\n\n\n\n<p>Same structure as the butterfly, but only border positions print a star. Add a border condition inside each wing.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>for (int i = 1; i &lt;= rows; i++) {\n\n&nbsp;&nbsp;&nbsp;&nbsp;for (int j = 1; j &lt;= i; j++) {\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (j == 1 || j == i) System.out.print(\"*\"); else System.out.print(\" \");\n\n&nbsp;&nbsp;&nbsp;&nbsp;}\n\n&nbsp;&nbsp;&nbsp;&nbsp;for (int j = 1; j &lt;= 2 * (rows - i); j++) System.out.print(\" \");\n\n&nbsp;&nbsp;&nbsp;&nbsp;for (int j = 1; j &lt;= i; j++) {\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (j == 1 || j == i) System.out.print(\"*\"); else System.out.print(\" \");\n\n&nbsp;&nbsp;&nbsp;&nbsp;}\n\n&nbsp;&nbsp;&nbsp;&nbsp;System.out.println();\n\n}\n\nfor (int i = rows; i &gt;= 1; i--) {\n\n&nbsp;&nbsp;&nbsp;&nbsp;for (int j = 1; j &lt;= i; j++) {\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (j == 1 || j == i) System.out.print(\"*\"); else System.out.print(\" \");\n\n&nbsp;&nbsp;&nbsp;&nbsp;}\n\n&nbsp;&nbsp;&nbsp;&nbsp;for (int j = 1; j &lt;= 2 * (rows - i); j++) System.out.print(\" \");\n\n&nbsp;&nbsp;&nbsp;&nbsp;for (int j = 1; j &lt;= i; j++) {\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (j == 1 || j == i) System.out.print(\"*\"); else System.out.print(\" \");\n\n&nbsp;&nbsp;&nbsp;&nbsp;}\n\n&nbsp;&nbsp;&nbsp;&nbsp;System.out.println();\n\n}\n\n```\n\n**Output (n=4):**\n\n```\n\n*&nbsp; &nbsp; &nbsp; *\n\n**&nbsp; &nbsp; **\n\n* *&nbsp; * *\n\n*&nbsp; **&nbsp; *\n\n*&nbsp; **&nbsp; *\n\n* *&nbsp; * *\n\n**&nbsp; &nbsp; **\n\n*&nbsp; &nbsp; &nbsp; *<\/code><\/pre>\n\n\n\n<p><strong>Concept tested:<\/strong> Border conditions on mirrored wings<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>15. Hollow Square Patterns<\/strong><\/h3>\n\n\n\n<p>A 2D grid where only the border cells print a star. The condition is simple, first row, last row, first column, or last column.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>for (int i = 1; i &lt;= rows; i++) {\n\n&nbsp;&nbsp;&nbsp;&nbsp;for (int j = 1; j &lt;= rows; j++) {\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (i == 1 || i == rows || j == 1 || j == rows) \/\/ border check\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.print(\"*\");\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.print(\" \");\n\n&nbsp;&nbsp;&nbsp;&nbsp;}\n\n&nbsp;&nbsp;&nbsp;&nbsp;System.out.println();\n\n}\n\n```\n\n**Output (n=5):**\n\n```\n\n*****\n\n* &nbsp; *\n\n* &nbsp; *\n\n* &nbsp; *\n\n*****<\/code><\/pre>\n\n\n\n<p><strong>Concept tested:<\/strong> <a href=\"https:\/\/codesignal.com\/learn\/courses\/multidimensional-arrays-and-their-traversal-in-java\/lessons\/grid-traversal-finding-ascending-paths-in-2d-arrays-in-java\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">2D grid traversal<\/a>, multi-condition border check<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Common Mistakes to Avoid in Interviews<\/strong><\/h2>\n\n\n\n<p>Even if you know the code, these slip-ups can cost you:<\/p>\n\n\n\n<ul>\n<li><strong>Off-by-one errors<\/strong> \u2014 check whether your loop starts at 0 or 1 and whether it ends at &lt; n or &lt;= n<\/li>\n\n\n\n<li><strong>Forgetting <\/strong><strong>System.out.println()<\/strong> \u2014 missing the newline after each row collapses your pattern into one line<\/li>\n\n\n\n<li><strong>Wrong space formula<\/strong> \u2014 for pyramids, spaces = rows - i, not i - 1 (they give different results for off-center alignment)<\/li>\n\n\n\n<li><strong>Explaining nothing<\/strong> \u2014 writing silent code in an interview is a red flag; narrate your loop logic as you write it<\/li>\n\n\n\n<li><strong>Hardcoding <\/strong><strong>n<\/strong> \u2014 always take n as user input via Scanner unless told otherwise<\/li>\n<\/ul>\n\n\n\n<p>Master Star Patterns in Java Full Stack Development with HCL GUVI\u2019s industry-relevant <a href=\"https:\/\/www.guvi.in\/zen-class\/java-full-stack-development-course\/?utm_source=blog&amp;utm_medium=hyperlink+&amp;utm_campaign=star-patterns-in-java-for-your-next-interview\" target=\"_blank\" rel=\"noreferrer noopener\">Java Full Stack Development course<\/a> designed by IIT-M &amp; 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.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Conclusion<\/strong><\/h2>\n\n\n\n<p>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.<\/p>\n\n\n\n<p>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.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>FAQs<\/strong><\/h2>\n\n\n<div id=\"rank-math-faq\" class=\"rank-math-block\">\n<div class=\"rank-math-list \">\n<div id=\"faq-question-1774876583103\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \"><strong>1. What is a star pattern program in Java?<\/strong>\u00a0<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>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.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1774876585868\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \"><strong>2. Why are pattern programs asked in Java interviews?<\/strong>\u00a0<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>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.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1774876589248\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \"><strong>3. What is the most commonly asked star pattern in interviews?<\/strong>\u00a0<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>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.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1774876594552\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \"><strong>4. What is the difference between a solid and hollow star pattern?<\/strong>\u00a0<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>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.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1774876600990\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \"><strong>5. Can I use while loops instead of for loops for star patterns?<\/strong>\u00a0<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>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.<\/p>\n\n<\/div>\n<\/div>\n<\/div>\n<\/div>","protected":false},"excerpt":{"rendered":"<p>Ever walked into a Java interview and blanked on a pattern question you&#8217;d seen a dozen times before? You&#8217;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&#8217;s exactly why interviewers love them. [&hellip;]<\/p>\n","protected":false},"author":22,"featured_media":71187,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[720,719],"tags":[],"views":"24058","authorinfo":{"name":"Lukesh S","url":"https:\/\/www.guvi.in\/blog\/author\/lukesh\/"},"thumbnailURL":"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2025\/01\/star-patterns-300x112.webp","jetpack_featured_media_url":"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2025\/01\/star-patterns.webp","_links":{"self":[{"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/posts\/71105"}],"collection":[{"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/users\/22"}],"replies":[{"embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/comments?post=71105"}],"version-history":[{"count":17,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/posts\/71105\/revisions"}],"predecessor-version":[{"id":105089,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/posts\/71105\/revisions\/105089"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/media\/71187"}],"wp:attachment":[{"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/media?parent=71105"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/categories?post=71105"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/tags?post=71105"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}