DSA interview question
The diamond is the first pattern built by combining two earlier patterns: a star pyramid (Pattern 7) on top of an inverted star pyramid (Pattern 8). Instead of deriving everything from scratch, the clean solution reuses the two row formulas you already have. Following the common judge convention (Striver's sheet and GeeksforGeeks), the widest row appears twice, once at the bottom of the top half and once at the top of the bottom half, giving 2n rows.
Example for n = 3:
*
***
*****
*****
***
*
vector<string> diamond(int n); // pyramid of n rows followed by an inverted pyramid of n rows
Input n | Output rows | Explanation |
|---|---|---|
| 1 | "*", "*" | Each half is a single star |
| 2 | " *", "***", "***", " *" | Widest row appears twice |
| 3 | " *", " ***", "*****", "*****", " ***", " *" | 2n = 6 rows |
(Confirm: this follows the convention where the middle row repeats, giving 2n rows. If the widest row should appear once, the bottom half starts one row later. No trailing spaces.)
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