Top 5 Pattern Problems in C++: Crack Your Next Coding Interview

9.06K 0 0 0 0

Problem #5: Number Pyramid

🔹 1. Problem Description

The Number Pyramid is a common C++ pattern problem where numbers are arranged in a pyramid-like structure. Unlike star patterns, this one focuses on number progression and formatting. It tests your understanding of:

  • Nested loops
  • Output alignment
  • Arithmetic progression
  • Conditional placement

Objective:

Given an input n, print a pyramid of n rows using increasing numbers row-wise.

Example (n = 5):

    1

   1 2

  1 2 3

 1 2 3 4

1 2 3 4 5

Each row starts from 1 and ends at the row number.


🔹 2. Flowchart

Start

|

|-- Input n

|

|-- For i = 1 to n:

|     Print (n - i) spaces

|     For j = 1 to i:

|         Print j

|     Print newline

|

End

Focus on two main tasks:

  • Leading spaces for centering
  • Incremental numbers per row

🔹 3. Algorithm

🔽 Step-by-Step:

  1. Start
  2. Read input n (number of rows)
  3. Loop i from 1 to n:
    • Print n - i spaces
    • Print numbers from 1 to i
  4. Print newline after each row
  5. End

Pseudocode:

Input: n

for i = 1 to n:

    print (n - i) spaces

    for j = 1 to i:

        print j

    print newline


🔹 4. C++ Code — Number Pyramid

#include <iostream>

using namespace std;

 

int main() {

    int n;

    cout << "Enter number of rows: ";

    cin >> n;

 

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

        // Print leading spaces

        for (int space = 1; space <= n - i; space++)

            cout << " ";

 

        // Print numbers

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

            cout << j << " ";

 

        cout << endl;

    }

 

    return 0;

}


🔹 5. Output for n = 5

    1

   1 2

  1 2 3

 1 2 3 4

1 2 3 4 5

Numbers increase per row, aligned in a centered pyramid.


🔹 6. Output Breakdown Table

Row

Spaces

Numbers Printed

1

4

1

2

3

1 2

3

2

1 2 3

4

1

1 2 3 4

5

0

1 2 3 4 5


🔹 7. Alternate Variations

🔁 Reverse Numbers:

    1

   2 1

  3 2 1

 4 3 2 1

5 4 3 2 1

Replace cout << j with cout << i - j + 1.


🔁 Continuous Number Pyramid:

    1

   2 3

  4 5 6

 7 8 9 10

11 12 13 14 15

Add a counter:

int num = 1;

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

    for (int space = 1; space <= n - i; space++)

        cout << " ";

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

        cout << num++ << " ";

    cout << endl;

}


🔹 8. Complexity & Considerations

Factor

Value

Time Complexity

O(n²)

Space Complexity

O(1)

Loops Used

3 (nested)

Output Lines

n

Alignment Type

Centered

This pattern builds logic around triangular formatting and number progression.


🔹 9. Common Pitfalls


Mistake

Solution

Skipping spaces

Use separate loop for n - i spaces

Forgetting endl

Required to start a new row

Misusing counter in variation

Always reset or increment carefully

Off-by-one loop errors

Loop from 1 to i not 0 to i-1

Back

FAQs


1. Why are pattern problems so common in interviews?

They test your mastery over loops, logic building, and output formatting under pressure.

2. What are the best C++ topics to master before patterns?

Understand for loops, nested loops, conditionals (if-else), and basic I/O.

3. Can I solve all pattern problems using just loops?

Yes. Most are based on nested loops (outer for rows, inner for columns/spaces/numbers).

4. How do I manage spacing and alignment in patterns?

Pay attention to the number of spaces before stars or digits. Use setw() or loops for space printing.

5. What is Floyd’s Triangle in C++?

It’s a right-angled triangular pattern using consecutive numbers from 1 onward in row-wise fashion.

6. How do I make patterns dynamic based on input size?

Use cin >> n and build loops that use n to determine number of rows/columns.

7. Are patterns important for advanced coding roles?

Theyre mostly asked at entry-level or internship roles, but understanding them helps in algorithmic thinking.

8. How do I debug misaligned patterns?

Use print statements for row and column counters; test smaller values first (n=3, n=5).

Tutorials are for educational purposes only, with no guarantees of comprehensiveness or error-free content; TuteeHUB disclaims liability for outcomes from reliance on the materials, recommending verification with official sources for critical applications.