PPractick

DSA interview question

Hard Pattern: Hollow Diamond of Stars

Easy · Algorithms · Topics: pattern building

The problem

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:

   *
  * *
 *   *
*     *
 *   *
  * *
   *

Function signature

vector<string> hollowDiamond(int n);  // 2n - 1 rows, leading spaces included, no trailing spaces

Test cases

Input nOutput rowsExplanation
1"*"A single star
2" *", "* *", " *"Middle row has a 1-space gap
3" *", " * *", "* *", " * *", " *"Gap grows 1, 3
47 rows as in the exampleGap 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.)

How interviewers phrase it

Clarifying questions to ask

  1. Does N mean the number of rows in the top half (including the widest row), or the total height?
  2. Should rows have trailing spaces to make a perfect rectangle, or end at the last star?
  3. Should I print directly or return the rows?
  4. What should happen for N = 1?

Follow-ups to expect

  1. "How did you work out the gap size?"
  2. "Why doesn't the first row have two stars?"
  3. "How do you avoid printing the middle row twice?"
  4. "How would you make it solid?"
  5. "What's the complexity?"

Practise questions like this with a real engineer

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 interview

Related questions

All 474 dsa interview questions

DSA mock interviews · System design interview questions · Free mock interview practice · Browse interviewers · Sign in