Mastering Patterns in DSA: Try this Pattern if you can

 

coding patterns

🧠 Why You Should Learn Patterns

Imagine you're trying to learn a new language. Before writing poems or novels, you need to master the alphabet and basic sentence structures.

In programming, pattern problems are like learning the alphabet. They help you:

  • Strengthen your logic and loop fundamentals.

  • Visualize what your code does.

  • Prepare for real coding interviews and advanced topics like Dynamic Programming.


🛠️ Getting Started with Pattern Problems

💡 General Strategy (A Teacher’s Trick):

To solve any pattern problem, ask yourself these three questions:

  1. How many lines/rows do I need to print?
    → This determines the outer loop.

  2. What do I need to print on each line?
    → This shapes your inner loop.

  3. Are there any spaces or special alignments?
    → Use spaces creatively for formatting!

Let’s now jump into coding patterns, starting from basics and gradually moving to advanced.


🔷 Basic Patterns

🧱 1. Square Pattern

📌 Goal: Print a square of 1’s for size n.

Example for n = 4:


1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

Code Explanation:


for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { cout << "1 "; } cout << endl; }

🧠 What's Happening?

  • Outer loop i runs for each row.

  • Inner loop j prints n times in each row.


🔺 2. Right-Angled Triangle

Example for n = 4:


1 1 2 1 2 3 1 2 3 4

Code:


for (int i = 1; i <= n; i++) { for (int j = 1; j <= i; j++) { cout << j << " "; } cout << endl; }

👨‍🏫 Teaching Tip:

  • Think of rows growing in length — from 1 number to 4 numbers.

  • The value j simply goes from 1 to i.


🔻 3. Inverted Triangle Pattern

Example for n = 4:


4 3 2 1 3 2 1 2 1 1

Code:


for (int i = n; i >= 1; i--) { for (int j = i; j >= 1; j--) { cout << j << " "; } cout << endl; }

🔍 Concept:

  • You start from a higher number and decrease both rows and the printed values.


🚀 Advanced Patterns

🔢 4. Floyd’s Triangle

Example for n = 4:


1 2 3 4 5 6 7 8 9 10

Code:


int number = 1; for (int i = 1; i <= n; i++) { for (int j = 1; j <= i; j++) { cout << number++ << " "; } cout << endl; }

🧠 Teaching Tip:

  • Keep one number variable and increase it after every print.

  • Let the outer loop grow line-by-line.


💎 5. Hollow Diamond Pattern

Example for n = 5:


* * * * * * * * * * * * * * * * * * *

Code:


// Top Half for (int i = 0; i < n; i++) { for (int j = 0; j < n - i - 1; j++) cout << " "; for (int j = 0; j <= i; j++) { if (j == 0 || j == i) cout << "* "; else cout << " "; } cout << endl; } // Bottom Half for (int i = n - 2; i >= 0; i--) { for (int j = 0; j < n - i - 1; j++) cout << " "; for (int j = 0; j <= i; j++) { if (j == 0 || j == i) cout << "* "; else cout << " "; } cout << endl; }

🧠 What’s Going On:

  • You split the diamond in two halves.

  • Use spaces to push stars to the center.

  • Print * only at the boundaries.


🎯 Practice Problems (Try These Yourself!)

✅ Print a number pyramid:


1 1 2 1 2 3 1 2 3 4

✅ Butterfly Pattern:


* * ** ** *** *** ******** ******** *** *** ** ** * *

✅ Hollow Square:


* * * * * * * * * * * * * *

Want code for these too? Just ask! 😄


📌 Cheat Sheet: Looping for Patterns

Concept                        Loop Logic                                                Notes
Outer LoopControls rowsUsually from 1 to n
Inner Loop                    Controls what is printed                            Stars, numbers, spaces
Nested LoopA loop inside anotherUsed for aligned patterns
Space HandlingOften printed before stars/numbersAdds symmetry

🎓 Final Thoughts

🎉 Congratulations! You now understand how to:

  • Break down pattern problems.

  • Use nested loops effectively.

  • Think visually like a coder!

Learning these patterns is not just about fun — it builds your logical thinking and prepares you for real programming challenges, from coding interviews to DSA mastery.


📘 What’s Next?

In the next part of this blog series, we’ll explore:

  • Star and Number Combinations

  • Symmetric & Asymmetric Patterns

  • Real Interview Pattern Questions

Comments