🔹 1. Problem Description
The Diamond Pattern is a classic shape problem in
coding interviews that tests your ability to work with:
It's visually appealing and technically enriching,
especially when you’re asked to build symmetrical shapes with precision.
✅ Objective:
Given an integer n, print a symmetrical diamond of 2n - 1
rows using *.
For example, n = 5 should print:
*
* *
* * *
* * * *
*
* * * *
* * * *
* * *
* *
*
🔹 2. Flowchart
Start
|
|--
Input n
|
|--
Upper Half: for i = 1 to n
| Print (n - i) spaces
| Print (2*i - 1) stars
|
|--
Lower Half: for i = n-1 to 1
| Print (n - i) spaces
| Print (2*i - 1) stars
|
End
✅ Note: Spaces help center the
diamond, and stars grow or shrink in odd numbers (1, 3, 5...)
🔹 3. Algorithm
🔽 Step-by-Step Approach:
✅ Pseudocode:
Input:
n
for
i = 1 to n:
print (n - i) spaces
print (2*i - 1) stars
for
i = n-1 to 1:
print (n - i) spaces
print (2*i - 1) stars
🔹 4. C++ Code — Diamond
Pattern
#include
<iostream>
using
namespace std;
int
main() {
int n;
cout << "Enter the number of
rows (half height): ";
cin >> n;
// Upper Half
for (int i = 1; i <= n; i++) {
for (int space = 1; space <= n - i;
space++)
cout << " ";
for (int star = 1; star <= 2 * i -
1; star++)
cout << "*";
cout << endl;
}
// Lower Half
for (int i = n - 1; i >= 1; i--) {
for (int space = 1; space <= n - i;
space++)
cout << " ";
for (int star = 1; star <= 2 * i -
1; star++)
cout << "*";
cout << endl;
}
return 0;
}
🔹 5. Output for n = 5
*
***
*****
*******
*********
*******
*****
***
*
✅ Total rows = 2n - 1, with the
center line being the widest.
🔹 6. Table Breakdown:
Row-wise Output
|
Row |
Spaces |
Stars |
|
1 |
4 |
1 |
|
2 |
3 |
3 |
|
3 |
2 |
5 |
|
4 |
1 |
7 |
|
5 |
0 |
9 |
|
6 |
1 |
7 |
|
7 |
2 |
5 |
|
8 |
3 |
3 |
|
9 |
4 |
1 |
🔹 7. C++ Enhancement:
Spaced Stars
If you want space between each star:
for
(int star = 1; star <= i; star++)
cout << "* ";
This will output:
*
* *
* * *
* * * *
*
* * * *
* * * *
* * *
* *
*
✅ Cleaner and easier to read.
🔹 8. Performance &
Complexity
|
Metric |
Value |
|
Time
Complexity |
O(n²) |
|
Space
Complexity |
O(1) |
|
Loops Used |
Nested |
|
Output Lines |
2n - 1 |
|
Characters/Row |
Varies |
🔹 9. Common Pitfalls
|
Mistake |
Fix |
|
Forgetting
newline |
Always add
cout << endl after each row |
|
Incorrect
space calculation |
Use n - i
spaces for centering |
|
Loop from 1
to n instead of n-1 |
Use n-1 to
avoid duplicate middle line |
|
Wrong 2*i - 1
expression |
Carefully
manage star count growth |
They test your mastery over loops, logic building, and output formatting under pressure.
Understand for loops, nested loops, conditionals (if-else), and basic I/O.
✅ Yes. Most are based on nested loops (outer for rows, inner for columns/spaces/numbers).
Pay attention to the number of spaces before stars or digits. Use setw() or loops for space printing.
It’s a right-angled triangular pattern using consecutive numbers from 1 onward in row-wise fashion.
Use cin >> n and build loops that use n to determine number of rows/columns.
✅ They’re mostly asked at entry-level or internship roles, but understanding them helps in algorithmic thinking.
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.
Kindly log in to use this feature. We’ll take you to the login page automatically.
LoginReady to take your education and career to the next level? Register today and join our growing community of learners and professionals.
Your experience on this site will be improved by allowing cookies. Read Cookie Policy
Your experience on this site will be improved by allowing cookies. Read Cookie Policy
Comments(0)