PPractick

DSA interview question

Pattern 9: Diamond of Stars

Easy · Algorithms · Topics: pattern building

The problem

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:

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

Function signature

vector<string> diamond(int n);  // pyramid of n rows followed by an inverted pyramid of n rows

Test cases

Input nOutput rowsExplanation
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.)

How interviewers phrase it

Clarifying questions to ask

  1. Should the widest row appear once or twice? (2n − 1 rows versus 2n rows.)
  2. Is N the height of each half?
  3. Trailing spaces allowed?

Follow-ups to expect

  1. "How would you print it without repeating the middle row?"
  2. "How would you make it hollow?"
  3. "Can you do it with a single loop over all rows?"
  4. "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