DSA interview question
This pattern is a sideways triangle: the rows grow from one star to n stars, then shrink back down to one. It's a right-angled triangle (Pattern 2) followed by an inverted one (Pattern 5) that shares the widest row, so there are 2n − 1 rows in total. The interesting part is that it can be written with a single loop over all rows if you can express "how many stars in row r" as one formula that rises and then falls.
Example for n = 3:
*
**
***
**
*
vector<string> halfDiamond(int n); // 2n - 1 rows: 1, 2, ..., n, ..., 2, 1 stars
Input n | Output rows | Explanation |
|---|---|---|
| 1 | "*" | A single row |
| 2 | "*", "**", "*" | Rises to 2 and falls back |
| 3 | "*", "**", "***", "**", "*" | Widest row appears once |
| 4 | 1, 2, 3, 4, 3, 2, 1 stars | 2n − 1 = 7 rows |
(Confirm: the widest row appears exactly once; rows are left-aligned with no separators.)
On Practick another engineer interviews you live for 60 minutes on a question from this bank. They have the full interviewer guide beside the call: the expected answer to every follow-up, hints to give if you stall, and a scoring rubric. You get the model solution once the session settles. It is free: interview someone for an hour and you have earned your own.
Book a free mock interviewAll 474 dsa interview questions
DSA mock interviews · System design interview questions · Free mock interview practice · Browse interviewers · Sign in