<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Pranav Arora on Medium]]></title>
        <description><![CDATA[Stories by Pranav Arora on Medium]]></description>
        <link>https://medium.com/@pranavarora.in?source=rss-382e52546f09------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*7AAN-47pCRkG5_jB5gViRw.png</url>
            <title>Stories by Pranav Arora on Medium</title>
            <link>https://medium.com/@pranavarora.in?source=rss-382e52546f09------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 22 Nov 2026 03:26:26 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@pranavarora.in/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Mastering Pattern Problems in Java: Alphanumeric Formations & Global State (Part 3)]]></title>
            <link>https://medium.com/@pranavarora.in/mastering-pattern-problems-in-java-alphanumeric-formations-global-state-part-3-bbe904b487cd?source=rss-382e52546f09------2</link>
            <guid isPermaLink="false">https://medium.com/p/bbe904b487cd</guid>
            <dc:creator><![CDATA[Pranav Arora]]></dc:creator>
            <pubDate>Mon, 25 May 2026 14:26:00 GMT</pubDate>
            <atom:updated>2026-05-25T14:26:00.727Z</atom:updated>
            <content:encoded><![CDATA[<p>Welcome to the grand finale of our pattern-solving series! In <strong>Part 1</strong>, we broke down solid grids using mathematical mapping. In <strong>Part 2</strong>, we mastered structural boundaries to craft complex hollow shapes.</p><p>Today, we are taking the ultimate step. We are leaving static asterisks (*) behind to tackle <strong>alphanumeric matrices and sequential tracking</strong>.</p><p>In technical coding interviews, these questions are highly favored. Why? Because they don’t just test your grid placement — they force you to manage state. You have to figure out not only <em>where</em> to print, but <em>what</em> to print as data structures change on every single cycle.</p><p>Let’s re-verify our foundation before diving in:</p><blockquote><strong><em>The 3-Rule Framework</em></strong></blockquote><ul><li><strong>The outer loop handles the number of rows</strong> that exist in the pattern.</li><li><strong>For each row, find the column count</strong> — determine whether it is dynamic or constant.</li><li><strong>Analyze each row to deduce formulas or structural relationships.</strong></li></ul><p>Let’s conquer the final frontier of pattern coding.</p><h3>1. The Numbered Right-Angle Triangle</h3><pre>1<br>12<br>123<br>1234<br>12345</pre><p>A clean transition point to begin thinking about data values. The geometry matches our earliest triangle patterns, but it introduces the concept of printing loop counters instead of static symbols.</p><h3>Java Implementation</h3><pre>import java.util.Scanner;</pre><pre>class Main {<br>    public static void main(String[] args) {<br>        Scanner sc = new Scanner(System.in);<br>        int n = sc.nextInt();<br>        <br>        // Rule 1: Outer loop handles total rows (n)<br>        for(int i = 1; i &lt;= n; i++){<br>            // Rule 2: Dynamic column count driven by row index &#39;i&#39;<br>            for(int j = 1; j &lt;= i; j++){<br>                // Rule 3: Relation maps directly to the current column loop state<br>                System.out.print(j);<br>            }<br>            System.out.println();<br>        }<br>    }<br>}</pre><h3>Framework Breakdown</h3><ul><li><strong>Rule 1 (Row Count):</strong> The shape spans a vertical height of n rows, handled by our outer loop up to i &lt;= n.</li><li><strong>Rule 2 (Column Behavior):</strong> The width of each individual row is completely <strong>dynamic</strong>. The number of characters on any line is exactly equal to the current line index i.</li><li><strong>Rule 3 (Deduce Relations):</strong> Look closely at how the numbers evolve. Every single line resets its starting position back to 1. As the inner loop advances horizontally across the grid, the value increments uniformly. Because this perfectly matches our column loop counter, we simply print our inner tracking variable j directly.</li></ul><h3>2. Floyd’s Triangle</h3><pre>1<br>2 3<br>4 5 6<br>7 8 9 10</pre><p>Floyd’s Triangle introduces a critical conceptual shift: <strong>maintaining a continuous global state variable</strong>. Unlike our previous pattern, the grid values do not reset when a new row begins.</p><h3>Java Implementation</h3><pre>import java.util.Scanner;</pre><pre>class Main {<br>    public static void main(String[] args) {<br>        Scanner sc = new Scanner(System.in);<br>        int n = sc.nextInt();<br>        <br>        // Declare a continuous tracker outside the loop scope<br>        int count = 1;<br>        <br>        // Rule 1: Outer loop for total rows<br>        for(int i = 1; i &lt;= n; i++){<br>            // Rule 2: Dynamic column width driven by &#39;i&#39;<br>            for(int j = 1; j &lt;= i; j++){<br>                // Rule 3: Print and increment global tracking variable<br>                System.out.print(count + &quot; &quot;);<br>                count++;<br>            }<br>            System.out.println();<br>        }<br>    }<br>}</pre><h3>Framework Breakdown</h3><ul><li><strong>Rule 1 (Row Count):</strong> Built systematically down over n rows, handled cleanly by our outer loop framework.</li><li><strong>Rule 2 (Column Behavior):</strong> The visual matrix shape is identical to a standard triangle. The active column print operations are <strong>dynamic</strong> and bounded by j &lt;= i.</li><li><strong>Rule 3 (Deduce Relations):</strong> Look at the sequence: 1, 2, 3, 4, 5... it acts as a non-stop running ledger across the entire execution life of the program. To achieve this, we declare int count = 1; entirely <strong>outside</strong> the loops. Inside the inner loop block, we print count and instantly increment it (count++). Because it lives outside the row cycle, it never resets.</li></ul><h3>3. The Character Right-Angle Triangle</h3><pre>A<br>AB<br>ABC<br>ABCD<br>ABCDE</pre><p>This pattern demonstrates the full flexibility of the framework, showing how it easily handles character primitives by leveraging under-the-hood ASCII mappings.</p><h3>Java Implementation</h3><pre>import java.util.Scanner;</pre><pre>class Main {<br>    public static void main(String[] args) {<br>        Scanner sc = new Scanner(System.in);<br>        int n = sc.nextInt();<br>        <br>        // Rule 1: Outer loop for rows<br>        for(int i = 1; i &lt;= n; i++){<br> // Reset character track point at the beginning of every single row<br>            char ch = &#39;A&#39;;<br>            <br>            // Rule 2: Dynamic columns bounded by &#39;i&#39;<br>            for(int j = 1; j &lt;= i; j++){<br>                // Rule 3: Output current letter value and increment<br>                System.out.print(ch);<br>                ch++;<br>            }<br>            System.out.println();<br>        }<br>    }<br>}</pre><h3>Framework Breakdown</h3><ul><li><strong>Rule 1 (Row Count):</strong> Climbs down to a vertical depth of n items, controlled via our standard outer loop limits.</li><li><strong>Rule 2 (Column Behavior):</strong> The layout expands dynamically line-by-line, requiring exactly i executions per row.</li><li><strong>Rule 3 (Deduce Relations):</strong> Analyze the lifecycle of the characters. The letters increment horizontally across columns, but the sequence completely resets to &#39;A&#39; on every single new row.</li><li>To implement this reset, we instantiate char ch = &#39;A&#39;; <strong>inside</strong> the outer loop right before the inner loop fires. As the columns step forward, we execute ch++. Because Java characters translate directly to sequential integer values behind the scenes, incrementing shifts it perfectly to the next alphabetical character.</li></ul><h3>4. The Inverted Character Right-Angle Triangle</h3><pre>E<br>ED<br>EDC<br>EDCB<br>EDCBA</pre><p>An excellent variation that forces engineers to reverse their mental map. It tests whether you can apply the framework to a sequence that counts backward down the alphabet.</p><h3>Java Implementation</h3><pre>import java.util.Scanner;</pre><pre>class Main {<br>    public static void main(String[] args) {<br>        Scanner sc = new Scanner(System.in);<br>        int n = sc.nextInt();<br>        <br>        // Rule 1: Outer loop for rows<br>        for(int i = 1; i &lt;= n; i++){<br>        // Reset character tracking point to the starting letter &#39;E&#39;<br>            char ch = &#39;E&#39;;<br>            <br>            // Rule 2: Dynamic columns bounded by &#39;i&#39;<br>            for(int j = 1; j &lt;= i; j++){<br>                // Rule 3: Output current letter value and decrement<br>                System.out.print(ch);<br>                ch--;<br>            }<br>            System.out.println();<br>        }<br>    }<br>}</pre><h3>Framework Breakdown</h3><ul><li><strong>Rule 1 (Row Count):</strong> Extends down to n total lines, governed cleanly by our outer loop bounds.</li><li><strong>Rule 2 (Column Behavior):</strong> Follows the standard triangular expansion, calculating exactly i active prints per line cycle.</li><li><strong>Rule 3 (Deduce Relations):</strong> Look at the step pattern. Each row resets to the highest threshold letter (&#39;E&#39;), meaning we instantiate char ch = &#39;E&#39;; <strong>inside</strong> the outer loop scope. As the inner loop advances columns horizontally, the alphabet shifts <em>backward</em>. By running ch-- inside the inner loop block, the underlying character integer value decreases step-by-step, generating the descending string cleanly.</li></ul><h3>5. The Numbered Palindromic Pyramid</h3><pre>    1<br>   121<br>  12321<br> 1234321<br>123454321</pre><p>The crown jewel challenge of sequential design. This problem forces developers to combine geometric spacing techniques from Part 2 with multi-stage loop tracking to create a perfect numerical palindrome.</p><h3>Java Implementation</h3><pre>import java.util.Scanner;</pre><pre>class Main {<br>    public static void main(String[] args) {<br>        Scanner sc = new Scanner(System.in);<br>        int n = sc.nextInt();<br>        <br>        // Rule 1: Outer loop for rows<br>        for(int i = 1; i &lt;= n; i++){<br>            <br>            // Phase A: Handle leading background spaces<br>            for(int spaces = 1; spaces &lt;= n - i; spaces++){<br>                System.out.print(&quot; &quot;);<br>            }<br>            <br>          // Phase B: Ascend counting loops up to peak row value &#39;i&#39;<br>            for(int j = 1; j &lt;= i; j++){<br>                System.out.print(j);<br>            }<br>            <br>       // Phase C: Descend counting loop down from &#39;i - 1&#39; back to 1<br>            for(int k = i - 1; k &gt;= 1; k--){<br>                System.out.print(k);<br>            }<br>            <br>            System.out.println();<br>        }<br>    }<br>}</pre><h3>Framework Breakdown</h3><ul><li><strong>Rule 1 (Row Count):</strong> Spans exactly n total lines vertically, seamlessly calculated by the main outer loop.</li><li><strong>Rule 2 (Column Behavior):</strong> Completely dynamic and layered. Every row must construct n - i empty blocks first, then transition into consecutive number streams.</li><li><strong>Rule 3 (Deduce Relations):</strong> This layout requires splitting data production into two explicit numeric zones matching a mathematical palindrome:</li><li><strong>The Ascendant Side (</strong><strong>j loop):</strong> Resets to 1 on every line and increases horizontally until it prints the line&#39;s peak number, which matches the row index i.</li><li><strong>The Descendant Side (</strong><strong>k loop):</strong> Takes over immediately following the peak print. Because the maximum number i is already out, this loop establishes its starting line bounds at <strong>i - 1</strong>, processing decrement iterations (k--) backwards until it hits 1.</li></ul><p>Pattern problems are frequently thrown at engineers in technical interviews not to test their memory, but to test their approach to <strong>spatial logic and decomposition</strong>.</p><p>When you stop guessing loop offsets and instead systematically break down the matrix into <strong>Row Count</strong>, <strong>Column Behavior</strong>, and <strong>Index Formulas</strong>, you can construct any grid imaginable. Keep this 3-rule playbook close on your next technical interview scratchpad.</p><p>Thank you for following along with this series, and happy coding!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=bbe904b487cd" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Mastering Pattern Problems in Java: Hollow Shapes & Advanced Logic (Part 2)]]></title>
            <link>https://medium.com/@pranavarora.in/mastering-pattern-problems-in-java-hollow-shapes-advanced-logic-part-2-7031150251b6?source=rss-382e52546f09------2</link>
            <guid isPermaLink="false">https://medium.com/p/7031150251b6</guid>
            <category><![CDATA[java-coding]]></category>
            <category><![CDATA[java-problem-solving]]></category>
            <category><![CDATA[javapattern]]></category>
            <category><![CDATA[java-loop]]></category>
            <category><![CDATA[pattern-problem]]></category>
            <dc:creator><![CDATA[Pranav Arora]]></dc:creator>
            <pubDate>Sun, 24 May 2026 13:56:00 GMT</pubDate>
            <atom:updated>2026-05-24T13:56:00.518Z</atom:updated>
            <content:encoded><![CDATA[<p>Welcome back to our pattern-solving series! In <strong>Part 1</strong>, we established our universal <strong>3-Rule Framework</strong> and used it to conquer solid blocks, inverted triangles, and full pyramids.</p><p>If you haven’t read Part 1 yet, I highly recommend checking it out first to understand our baseline matrix principles.</p><p>Today, we are stepping into the deep end. We are shifting from solid shapes to <strong>hollow boundaries and compound shapes</strong>. This is the exact territory where many coding students drop their analytical framework and fall back into frustrating guesswork.</p><p>But as you’re about to see, our three rules adapt perfectly to even the most complex geometric structures. Remind yourself of our core principles before we begin:</p><blockquote><strong><em>The 3-Rule Framework</em></strong></blockquote><ul><li><strong>The outer loop handles the number of rows</strong> that exist in the patter</li><li><strong>For each row, find the column count</strong> — determine whether it is dynamic or constant.</li><li><strong>Analyze each row to deduce formulas or structural relationships</strong></li></ul><p>Let’s apply these rules to advanced code.</p><h3>1. The Hollow Rectangle</h3><pre>******<br>*    *<br>*    *<br>******</pre><p>Instead of filling every single cell in our coordinate system, a hollow shape requires us to treat row boundaries as distinct events using structural if-else branches.</p><h3>Java Implementation</h3><pre>import java.util.Scanner;</pre><pre>class Main {<br>    public static void main(String[] args) {<br>        Scanner sc = new Scanner(System.in);<br>        int n = sc.nextInt();<br>        <br>        // Rule 1: Outer loop handles total rows (n)<br>        for(int i = 1; i &lt;= n; i++){<br>            <br>            // Rule 2 &amp; 3: Structural check for boundary rows (Top &amp; Bottom)<br>            if(i == 1 || i == n){<br>                for(int j = 1; j &lt;= 6; j++){<br>                    System.out.print(&quot;*&quot;);<br>                }<br>                System.out.println();<br>            }<br>           // Rule 2 &amp; 3: Hollow interior processing for middle rows<br>            else {<br>                System.out.print(&quot;*&quot;);<br>                for(int j = 1; j &lt;= n; j++){<br>                    System.out.print(&quot; &quot;);<br>                }<br>                System.out.print(&quot;*&quot;);<br>                System.out.println();<br>            }<br>        }<br>    }<br>}</pre><h3>Framework Breakdown</h3><ul><li><strong>Rule 1 (Row Count):</strong> The pattern spans exactly n lines vertically, meaning our outer loop safely counts from i = 1 up to n.</li><li><strong>Rule 2 (Column Behavior):</strong> The column structures are constant in terms of overall layout width, but they are split by row types. The top and bottom lines are fully packed with 6 stars, while the middle rows are hollow blocks containing a fixed count of spaces.</li><li><strong>Rule 3 (Deduce Relations):</strong> We use a simple structural check on the row index i. If we are on the first line (i == 1) or the last line (i == n), we fire a clean loop to output 6 stars. Otherwise, we manually print a starting star, fill the gap using a fixed loop printing n spaces, and append the closing star.</li></ul><h3>2. The Hollow Right-Angle Triangle</h3><pre>*<br>**<br>* *<br>*  *<br>*****</pre><p>This problem forces us to manage hollow structural logic within a layout that is expanding dynamically line-by-line.</p><h3>Java Implementation</h3><pre>import java.util.Scanner;</pre><pre>class Main {<br>    public static void main(String[] args) {<br>        Scanner sc =  new Scanner(System.in);<br>        int n = sc.nextInt();<br>        <br>        // Rule 1: Outer loop for rows<br>        for(int i = 1; i &lt;= n; i++){<br>            <br>            // Rule 2 &amp; 3: Identify full boundary rows (Row 1, 2, and Base)<br>            if(i == 1 || i == n || i == 2){<br>                for(int j = 1; j &lt;= i; j++){<br>                    System.out.print(&quot;*&quot;);<br>                }<br>                System.out.println();<br>            }<br>            // Rule 2 &amp; 3: Process intermediate dynamic hollow rows<br>            else {<br>                System.out.print(&quot;*&quot;);<br>                for(int j = 1; j &lt;= i - 2; j++){<br>                    System.out.print(&quot; &quot;);<br>                }<br>                System.out.print(&quot;*&quot;);<br>                System.out.println();<br>            }<br>        }<br>    }<br>}</pre><h3>Framework Breakdown</h3><ul><li><strong>Rule 1 (Row Count):</strong> The triangle spans vertically to a height of n, handled by our outer loop up to i &lt;= n.</li><li><strong>Rule 2 (Column Behavior):</strong> The width of the rows is highly dynamic. However, rows 1, 2, and the final row n are completely solid, while rows between 2 and n are hollow.</li><li><strong>Rule 3 (Deduce Relations):</strong> If i matches our solid boundaries (1, 2, or n), we print exactly i stars. For all other internal rows, we print a leading star, run a space-filling loop bounded by i - 2, and cap it off with a star.</li><li><strong>Why </strong><strong>i - 2?</strong> A standard solid right-angle row prints i total items. Because we manually use up 2 positions to print the outer border stars (System.out.print(&quot;*&quot;)), we subtract 2 from the loop limit to leave the remaining interior perfectly hollow.</li></ul><h3>3. The Hollow Pyramid</h3><pre>    *<br>   * *<br>  *   *<br> *     *<br>*********</pre><p>This is an excellent interview problem that balances two major hurdles at once: shifting the layout horizontally with leading spaces, and tracking a hollow interior via an odd-number sequence equation.</p><h3>Java Implementation</h3><pre>import java.util.Scanner;</pre><pre>class Main {<br>    public static void main(String[] args) {<br>        Scanner sc =  new Scanner(System.in);<br>        int n = sc.nextInt();<br>        <br>        // Rule 1: Outer loop for rows<br>        for(int i = 1; i &lt;= n; i++){<br>            <br>            // Part A: Print leading dynamic alignment spaces<br>            for(int spaces = 1; spaces &lt;= n - i; spaces++){<br>                System.out.print(&quot; &quot;);<br>            }<br>            <br>            // Part B: Structural checks for pyramid star boundaries<br>            if(i == 1 || i == n){<br>                for(int j = 1; j &lt;= 2 * i - 1; j++){<br>                    System.out.print(&quot;*&quot;);<br>                }<br>                System.out.println();<br>            }<br>            else {<br>                System.out.print(&quot;*&quot;);<br>                for(int j = 1; j &lt;= 2 * i - 3; j++){<br>                    System.out.print(&quot; &quot;);<br>                }<br>                System.out.print(&quot;*&quot;);<br>                System.out.println();<br>            }<br>        }<br>    }<br>}</pre><h3>Framework Breakdown</h3><ul><li><strong>Rule 1 (Row Count):</strong> Vertically built over n rows, handled by our classic outer loop layout.</li><li><strong>Rule 2 (Column Behavior):</strong> Multi-stage and completely dynamic. Every single line requires an initial sequence of blank spaces followed by a custom arrangement of symbols depending on the row index i.</li><li><strong>Rule 3 (Deduce Relations):</strong> No matter what type of row it is, it always receives exactly n - i leading spaces to keep the pyramid centered. For the stars: if it&#39;s the tip (i == 1) or base (i == n), we use the odd number formula to run a solid row of 2 * i - 1 stars. For intermediate rows, we print 1 star, run a space loop up to 2 * i - 3, and print the closing star.</li><li><strong>Why </strong><strong>2 * i - 3?</strong> A solid pyramid row spans 2 * i - 1 positions. Since we take up 2 positions manually by printing the standalone starting star and closing star, we subtract 2 from the total width formula: (2 * i - 1) - 2 = 2 * i - 3.</li></ul><h3>4. The Hollow Diamond</h3><pre>    *<br>   * *<br>  *   *<br> *     *<br>*       *<br> *     *<br>  *   *<br>   * *<br>    *</pre><p>The absolute crown jewel of pattern logic questions. Instead of panicking over the complex geometry, we break it down horizontally into an upper growing hemisphere and a lower shrinking hemisphere.</p><h3>Java Implementation</h3><pre>import java.util.Scanner;</pre><pre>class Main {<br>    public static void main(String[] args) {<br>        Scanner sc =  new Scanner(System.in);<br>        int n = sc.nextInt();<br>        <br>        // UPPER HEMISPHERE (Growing Pyramid)<br>        for(int i = 1; i &lt;= n; i++){<br>            for(int spaces = 1; spaces &lt;= n - i; spaces++){<br>                System.out.print(&quot; &quot;);<br>            }<br>            if(i == 1){<br>                System.out.print(&quot;*&quot;);<br>                System.out.println();<br>            }<br>            else {<br>                System.out.print(&quot;*&quot;);<br>                for(int sp = 1; sp &lt;= 2 * i - 3; sp++){<br>                    System.out.print(&quot; &quot;);<br>                }<br>                System.out.print(&quot;*&quot;);<br>                System.out.println();<br>            }<br>        }<br>        <br>        // LOWER HEMISPHERE (Inverted Shrinking Pyramid)<br>        for(int i = 1; i &lt;= n - 1; i++){<br>            for(int j = 1; j &lt;= i; j++){<br>                System.out.print(&quot; &quot;);<br>            }<br>            if(i == n - 1){<br>                System.out.print(&quot;*&quot;);<br>            }<br>            else {<br>                System.out.print(&quot;*&quot;);<br>                for(int j = 1; j &lt;= 2 * (n - i) - 3; j++){<br>                    System.out.print(&quot; &quot;);<br>                }<br>                System.out.print(&quot;*&quot;);<br>                System.out.println();<br>            }<br>        }<br>    }<br>}</pre><h3>Framework Breakdown</h3><ul><li><strong>Rule 1 (Row Count):</strong> Split across two independent outer loops. The upper loop runs n times to expand the diamond, and the lower loop runs n - 1 times to cleanly taper the base back to a single point.</li><li><strong>Rule 2 (Column Behavior):</strong> Highly dynamic multi-stage structures. Both halves manage an alternating play between background alignment spaces and boundary star symbols.</li><li><strong>Rule 3 (Deduce Relations):</strong></li><li><strong>Top Half:</strong> Follows the exact same breakdown as our hollow pyramid — printing n - i leading spaces, and checking if i == 1 for the standalone tip star.</li><li><strong>Bottom Half:</strong> The leading alignment spaces grow linearly with the row counter (j &lt;= i). For the interior gap, we apply the inverse calculation 2 * (n - i) - 3. As i increases, this calculation forces the inner loop boundaries to count downward, mirroring the upper hemisphere&#39;s dimensions perfectly in reverse.</li></ul><h3>Part 2 Takeaways</h3><p>Hollow pattern questions can look incredibly messy at first glance, but they all boil down to a simple mathematical secret: <strong>managing inner boundaries</strong>.</p><p>In our final chapter, <strong>Part 3</strong>, we are going to dive into character matrices, alphanumeric tracking, and mathematical grids like <strong>Pascal’s Triangle</strong> and <strong>Floyd’s Triangle</strong>.</p><p>See you there, and keep coding!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7031150251b6" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Mastering Pattern Problems in Java: The Only Framework You’ll Ever Need (Part 1)]]></title>
            <link>https://medium.com/@pranavarora.in/mastering-pattern-problems-in-java-the-only-framework-youll-ever-need-part-1-48dfd4e514b4?source=rss-382e52546f09------2</link>
            <guid isPermaLink="false">https://medium.com/p/48dfd4e514b4</guid>
            <category><![CDATA[loops-in-java]]></category>
            <category><![CDATA[pattern-problem]]></category>
            <category><![CDATA[javapattern]]></category>
            <category><![CDATA[logic-building]]></category>
            <dc:creator><![CDATA[Pranav Arora]]></dc:creator>
            <pubDate>Sun, 24 May 2026 06:18:21 GMT</pubDate>
            <atom:updated>2026-05-26T08:19:53.199Z</atom:updated>
            <content:encoded><![CDATA[<p>If you have ever prepared for a technical interview or taken a foundational Java programming course, you have run into them: <strong>Pattern Problems</strong>.</p><pre>* * * *<br>* * * *<br>* * * *<br>* * * *</pre><p>At first glance, trying to print combinations of stars, spaces, and numbers can feel like pure guesswork. You tweak an i here, add a - 1 there, compile it, watch the output break, and repeat.</p><p>But printing patterns isn’t a test of your guessing skills. It’s a test of your ability to break a matrix down into rows and columns. Once you learn a universal <strong>3-Rule Framework</strong>, you can solve almost any grid-based pattern problem instantly without losing your mind.</p><p>In this guide, I will walk you through that framework and give you clean, production-ready Java code for the foundational pattern shapes.</p><h3>The Universal 3-Rule Framework</h3><p>Every pattern problem is fundamentally a 2D grid, made up of horizontal rows and vertical columns. To solve them systematically, use these three basic principles:</p><ul><li><strong>The outer loop is nothing but the number of rows that exist in the pattern.</strong> How many total horizontal lines do you need to print? This controls your outer loop boundary.</li><li><strong>For each row, first of all find the column count whether it is dynamic or constant.</strong> Identify if your line width stays fixed or changes as you move down the grid.</li><li><strong>Analyze each row and try to deduce formulas or relations.</strong> Find the exact mathematical connection between the current row index and what needs to be printed.</li></ul><p>Let’s apply these rules to code.</p><h3>1. The Solid Rectangle</h3><pre>* * * * <br>* * * * <br>* * * * <br>* * * *</pre><p>The absolute baseline. If you want to print a simple rectangle, your loops operate quite plainly because nothing changes from line to line.</p><h3>Code:</h3><pre>import java.util.Scanner;</pre><pre>class Main {<br>    public static void main(String[] args) {<br>        Scanner sc = new Scanner(System.in);<br>        int n = sc.nextInt(); // Total rows<br>        <br>        // Rule 1: Outer loop handles total rows (n)<br>        for(int i = 1; i &lt;= n; i++){<br>            // Rule 2 &amp; 3: Constant column count of 4<br>            for(int j = 1; j &lt;= 4; j++){<br>                System.out.print(&quot;* &quot;);<br>            }<br>            System.out.println();<br>        }<br>    }<br>}</pre><h3>Framework Breakdown:</h3><ul><li><strong>Rule 1 (Rows):</strong> The pattern has n total rows, so our outer loop runs from i = 1 up to n.</li><li><strong>Rule 2 (Columns):</strong> No matter which row you are on, the number of stars remains exactly <strong>constant</strong>. It always prints 4 stars, meaning the inner loop boundary is a fixed j &lt;= 4.</li><li><strong>Rule 3 (Relations):</strong> Since the columns don’t change based on the row index, there is no complex mathematical formula needed here. Every single position gets a star.</li></ul><h3>2. The Solid Square</h3><pre>* * * * *<br>* * * * *<br>* * * * *<br>* * * * *<br>* * * * *</pre><p>A natural transition from the rectangle that shifts our hardcoded boundary values into a variable-driven grid.</p><h3>Code:</h3><pre>import java.util.Scanner;</pre><pre>class Main {<br>    public static void main(String[] args) {<br>        Scanner sc = new Scanner(System.in);<br>        int n = sc.nextInt(); // Matrix size (n x n)<br>        <br>        // Rule 1: Outer loop handles total rows (n)<br>        for(int i = 1; i &lt;= n; i++){<br>            // Rule 2 &amp; 3: Constant column count driven by input (n)<br>            for(int j = 1; j &lt;= n; j++){<br>                System.out.print(&quot;* &quot;);<br>            }<br>            System.out.println();<br>        }<br>    }<br>}</pre><h3>Framework Breakdown:</h3><ul><li><strong>Rule 1 (Rows):</strong> The pattern requires n horizontal lines, so our outer loop runs from i = 1 to i &lt;= n.</li><li><strong>Rule 2 (Columns):</strong> The column count is <strong>constant</strong> for every line. However, instead of being a hardcoded number like 4, the width matches the user input. Every single row prints exactly n characters (j &lt;= n).</li><li><strong>Rule 3 (Relations):</strong> Because the total rows equal the total columns, we have a uniform $n \times n$ matrix grid where every single position is filled with a star.</li></ul><h3>3. The Solid Right-Angle Triangle</h3><pre>* <br>* * <br>* * * <br>* * * * <br>* * * * *</pre><p>This marks a crucial milestone in our pattern-solving journey: shifting from <strong>constant</strong> columns to <strong>dynamic</strong> columns.</p><h3>Code:</h3><pre>import java.util.Scanner;</pre><pre>class Main {<br>    public static void main(String[] args) {<br>        Scanner sc = new Scanner(System.in);<br>        int n = sc.nextInt();<br>        <br>        // Rule 1: Outer loop for rows<br>        for(int i = 1; i &lt;= n; i++){<br>           // Rule 2 &amp; 3: Dynamic columns dependent on row index &#39;i&#39;<br>            for(int j = 1; j &lt;= i; j++){<br>                System.out.print(&quot;* &quot;);<br>            }<br>            System.out.println();<br>        }<br>    }<br>}</pre><h3>Framework Breakdown:</h3><ul><li><strong>Rule 1 (Rows):</strong> The triangle has a height of n rows, so our outer loop runs up to i &lt;= n.</li><li><strong>Rule 2 (Columns):</strong> The number of stars changes on every single line, meaning our column count is <strong>dynamic</strong>.</li><li><strong>Rule 3 (Relations):</strong> Let’s deduce the relation between the row number (i) and the number of stars printed:</li><li>Row 1 (i = 1) -&gt; Prints 1 star</li><li>Row 2 (i = 2) -&gt; Prints 2 stars</li><li>Row i -&gt; Prints exactly <strong>i</strong> stars.</li><li>Because the number of columns is exactly equal to the current row number, we set our inner loop condition directly to j &lt;= i.</li></ul><h3>4. The Inverted Right-Angle Triangle</h3><pre>* * * * * <br>* * * * <br>* * * <br>* * <br>*</pre><p>Beginners often struggle here because they try to guess the loop limits to make the stars shrink. Our third rule completely removes that guesswork.</p><h3>Code:</h3><pre>import java.util.Scanner;</pre><pre>class Main {<br>    public static void main(String[] args) {<br>        Scanner sc = new Scanner(System.in);<br>        int n = sc.nextInt();<br>        <br>        // Rule 1: Outer loop for rows<br>        for(int i = 1; i &lt;= n; i++){<br>            // Rule 2 &amp; 3: Dynamic columns using a deduced formula<br>            for(int j = 1; j &lt;= n - i + 1; j++){<br>                System.out.print(&quot;* &quot;);<br>            }<br>            System.out.println();<br>        }<br>    }<br>}</pre><h3>Framework Breakdown:</h3><ul><li><strong>Rule 1 (Rows):</strong> The pattern still consists of n total lines from top to bottom, so the outer loop stays as i &lt;= n.</li><li><strong>Rule 2 (Columns):</strong> The number of stars decreases as we move down. The column count is highly <strong>dynamic</strong>.</li><li><strong>Rule 3 (Relations):</strong> Let’s map out the relationship between row number (i) and stars printed (assuming n = 5):</li><li>Row 1 (i = 1) -&gt; Prints 5 stars</li><li>Row 2 (i = 2) -&gt; Prints 4 stars</li><li>Row i -&gt; Prints exactly <strong>n - i + 1</strong> stars.</li><li>We plug that formula straight into our inner loop condition: j &lt;= n - i + 1.</li></ul><h3>5. The Solid Rhombus</h3><pre>    * * * * *<br>   * * * * *<br>  * * * * *<br> * * * * *<br>* * * * *</pre><p>This pattern forces us to handle <em>two</em> different column behaviors in a single row: one dynamic (spaces) and one constant (stars).</p><h3>Code:</h3><pre>import java.util.Scanner;</pre><pre>class Main {<br>    public static void main(String[] args) {<br>        Scanner sc = new Scanner(System.in);<br>        int n = sc.nextInt();<br>        <br>        // Rule 1: Outer loop for rows<br>        for(int i = 1; i &lt;= n; i++){<br>            // Rule 2 &amp; 3 (Part A): Dynamic spaces loop<br>            for(int spaces = 1; spaces &lt;= n - i; spaces++){<br>                System.out.print(&quot; &quot;);<br>            }<br>            // Rule 2 &amp; 3 (Part B): Constant stars loop<br>            for(int stars = 1; stars &lt;= 5; stars++){<br>                System.out.print(&quot;*&quot;);<br>            }<br>            System.out.println();<br>        }<br>    }<br>}</pre><h3>Framework Breakdown:</h3><ul><li><strong>Rule 1 (Rows):</strong> Vertically, the shape spans exactly n lines, controlled by i &lt;= n.</li><li><strong>Rule 2 (Columns):</strong> Each row is split into two parts: leading spaces, then stars. The blank spaces are <strong>dynamic</strong>, while the stars are <strong>constant</strong> (fixed at 5 stars).</li><li><strong>Rule 3 (Relations):</strong> Let’s map out both parts across the rows (assuming n = 5):</li><li>Row 1 (i = 1) -&gt; Prints 4 spaces and 5 stars.</li><li>Row 2 (i = 2) -&gt; Prints 3 spaces and 5 stars.</li><li>Row i -&gt; Prints exactly <strong>n - i</strong> spaces and a fixed <strong>5</strong> stars.</li><li>We write two consecutive inner loops under our outer loop to manage these distinct sections smoothly.</li></ul><h3>6. The Solid Pyramid</h3><pre>    *<br>   ***<br>  *****<br> *******<br>*********</pre><p>The grand finale for Part 1. This iconic pattern features two separate inner loops that are both completely dynamic.</p><h3>Code:</h3><pre>import java.util.Scanner;</pre><pre>class Main {<br>    public static void main(String[] args) {<br>        Scanner sc = new Scanner(System.in);<br>        int n = sc.nextInt();<br>        <br>        // Rule 1: Outer loop for rows<br>        for(int i = 1; i &lt;= n; i++){<br>            // Rule 2 &amp; 3 (Part A): Dynamic spaces loop<br>            for(int spaces = 1; spaces &lt;= n - i; spaces++){<br>                System.out.print(&quot; &quot;);<br>            }<br>            // Rule 2 &amp; 3 (Part B): Dynamic stars loop<br>            for(int stars = 1; stars &lt;= 2 * i - 1; stars++){<br>                System.out.print(&quot;*&quot;);<br>            }<br>            System.out.println();<br>        }<br>    }<br>}</pre><h3>Framework Breakdown:</h3><ul><li><strong>Rule 1 (Rows):</strong> The pyramid is built vertically over n rows (i &lt;= n).</li><li><strong>Rule 2 (Columns):</strong> Every row contains leading spaces followed by stars. Both behaviors change line by line, meaning both are completely <strong>dynamic</strong>.</li><li><strong>Rule 3 (Relations):</strong> Let’s calculate the arithmetic pattern across the grid (assuming n = 5):</li><li>Row 1 (i = 1) -&gt; Prints 4 spaces and 1 star</li><li>Row 2 (i = 2) -&gt; Prints 3 spaces ($5 - 2$) and 3 stars</li><li>Row i -&gt; Prints exactly <strong>n - i</strong> spaces and <strong>2 * i - 1</strong> stars.</li><li>Using the odd number sequence formula, we establish total control over the star placement loop boundaries.</li></ul><h3>7. The Inverted Pyramid</h3><pre>*********<br> *******<br>  *****<br>   ***<br>    *</pre><p>The natural mirror counterpart to the traditional pyramid. It tests your ability to reverse loop parameters accurately.</p><h3>Code:</h3><pre>import java.util.Scanner;</pre><pre>class Main {<br>    public static void main(String[] args) {<br>        Scanner sc = new Scanner(System.in);<br>        int n = sc.nextInt();<br>        <br>        // Rule 1: Outer loop for rows<br>        for(int i = 1; i &lt;= n; i++){<br>            // Rule 2 &amp; 3 (Part A): Dynamic spaces loop<br>            for(int j = 1; j &lt;= i - 1; j++){<br>                System.out.print(&quot; &quot;);<br>            }<br>            // Rule 2 &amp; 3 (Part B): Dynamic stars loop<br>            for(int j = 1; j &lt;= 2 * n - 2 * i + 1; j++){<br>                System.out.print(&quot;*&quot;);<br>            }<br>            System.out.println();<br>        }<br>    }<br>}</pre><h3>Framework Breakdown:</h3><ul><li><strong>Rule 1 (Rows):</strong> Even though the pyramid is upside down, it still occupies exactly n vertical lines. The outer loop counts up cleanly using i &lt;= n.</li><li><strong>Rule 2 (Columns):</strong> Just like the upright pyramid, each row contains leading spaces and stars. However, as you move down, spaces grow while stars shrink. Both are highly <strong>dynamic</strong>.</li><li><strong>Rule 3 (Relations):</strong> Let’s map out the reverse math across rows (assuming n = 5):</li><li>Row 1 (i = 1) -&gt; Prints 0 spaces and 9 stars</li><li>Row 2 (i = 2) -&gt; Prints 1 space and 7 stars</li><li>Row i -&gt; Prints exactly <strong>i - 1</strong> spaces and <strong>2 * n - 2 * i + 1</strong> stars.</li><li>Plugging these conditions into sequential loops perfectly renders the shrinking odd-number shape.</li></ul><h3>What’s Next?</h3><p>Once you look past the visual shapes and focus entirely on <strong>index relationships</strong>, pattern problems change from frustrating guesswork into simple logic exercises.</p><p>In <strong>Part 2</strong>, we are going to kick it up a notch. We will use this exact same framework to tackle advanced, boundary-based variations, including <strong>Hollow Blocks</strong>, <strong>The Butterfly Pattern</strong>, and <strong>Diamond Formations</strong>. Stay tuned, and happy coding!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=48dfd4e514b4" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>