DSA interview question
The college coding club prints a logo on its welcome screen: a hollow diamond made of stars, with stars only on the outline and spaces inside. The club secretary wants it generated for any size n, where n is the number of rows from the top point down to the widest row. This pattern is a good "hard" exercise because it combines three ideas from easier patterns: leading spaces that shrink row by row, an inside gap that grows, and a mirrored bottom half that must not repeat the middle row.
Example for n = 4:
*
* *
* *
* *
* *
* *
*
vector<string> hollowDiamond(int n); // 2n - 1 rows, leading spaces included, no trailing spaces
Input n | Output rows | Explanation |
|---|---|---|
| 1 | "*" | A single star |
| 2 | " *", "* *", " *" | Middle row has a 1-space gap |
| 3 | " *", " * *", "* *", " * *", " *" | Gap grows 1, 3 |
| 4 | 7 rows as in the example | Gap grows 1, 3, 5; bottom mirrors top without repeating the widest row |
(Confirm: the output has 2n − 1 rows; the first and last rows have a single star; rows have 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