A curated collection of 22 classic pattern-printing programs, commonly asked in coding interviews and an excellent starting point for anyone beginning their Data Structures & Algorithms (DSA) journey.
Pattern problems are one of the best ways to build a strong foundation in loops, nested iteration, and logical thinking — core skills that carry over into more advanced DSA topics like recursion, backtracking, and dynamic programming.
- Beginner-friendly — no prior DSA experience required
- Covers the most frequently asked pattern problems in interviews
- Each pattern includes intuition, approach, and complexity analysis
- Helps build intuition for nested loops and iteration control
- A great warm-up before diving into arrays, strings, and recursion
| # | Pattern Name | Difficulty |
|---|---|---|
| 1 | Square Pattern | Easy |
| 2 | Right-Angled Triangle (Numbers) | Easy |
| 3 | Right-Angled Triangle (Stars) | Easy |
| 4 | Inverted Right-Angled Triangle | Easy |
| 5 | Pyramid Pattern | Easy |
| 6 | Inverted Pyramid Pattern | Easy |
| 7 | Diamond Pattern | Medium |
| 8 | Hollow Square Pattern | Medium |
| 9 | Hollow Triangle Pattern | Medium |
| 10 | Number Triangle (Increasing) | Easy |
| 11 | Number Triangle (Reverse) | Easy |
| 12 | Floyd's Triangle | Easy |
| 13 | Pascal's Triangle | Medium |
| 14 | Butterfly Pattern | Medium |
| 15 | Zig-Zag Pattern | Medium |
| 16 | Alphabet Triangle | Easy |
| 17 | Alphabet Pyramid | Medium |
| 18 | Hollow Diamond Pattern | Medium |
| 19 | Number Pyramid (Repeated Row Number) | Medium |
| 20 | Palindromic Number Pattern | Hard |
| 21 | Spiral Number Pattern | Hard |
| 22 | Sandglass / Hourglass Pattern | Hard |
Update the table above with the exact names/order of your 22 implementations.
Intuition: Since the number of rows and columns are equal, two nested loops suffice — the outer loop for rows and the inner loop for printing N stars per row.
Approach:
- Take an integer
Nas input to define the size of the square. - Use a loop from
0toN-1to represent each row. - Inside that loop, use another loop from
0toN-1to print stars in the current row. - Print
"* "during each inner loop iteration to form the row. - After each inner loop completes, move to the next line.
Complexity:
- Time:
O(N²) - Space:
O(1)
Intuition: Each row contains numbers starting from 1 up to the row number — row 1 has 1, row 2 has 1 2, row 3 has 1 2 3, and so on until N.
Approach:
- Use an outer loop
ifrom1toNfor rows. - For each row, use an inner loop
jfrom1toito print numbers. - Each row prints numbers starting from
1up to the current row index. - After printing each row, move to the next line.
Complexity:
- Time:
O(N²), since the outer loop runsNtimes and the inner loop runs up toitimes for each row. - Space:
O(1), since only loop variables are used.
# Clone the repository
git clone <your-repo-url>
# Navigate into the project
cd pattern-programs
# Run any pattern file
python patternX.pyThis repository is ideal for:
- Students starting their DSA/coding interview prep
- Beginners learning loops and nested iteration
- Anyone who wants daily practice with simple, visual problems before moving to arrays, strings, and recursion
Contributions are welcome! If you'd like to add a new pattern or improve an existing solution:
- Fork the repository
- Create a new branch (
git checkout -b add-new-pattern) - Commit your changes
- Open a pull request
This project is open-source and available under the MIT License.
If you found this helpful for your DSA journey, consider giving the repository a star!