PPractick

DSA interview question

Pattern 10: Half Diamond of Stars

Easy · Algorithms · Topics: pattern building

The problem

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:

*
**
***
**
*

Function signature

vector<string> halfDiamond(int n);  // 2n - 1 rows: 1, 2, ..., n, ..., 2, 1 stars

Test cases

Input nOutput rowsExplanation
1"*"A single row
2"*", "**", "*"Rises to 2 and falls back
3"*", "**", "***", "**", "*"Widest row appears once
41, 2, 3, 4, 3, 2, 1 stars2n − 1 = 7 rows

(Confirm: the widest row appears exactly once; rows are left-aligned with no separators.)

How interviewers phrase it

Clarifying questions to ask

  1. Should the widest row appear once or twice?
  2. Left-aligned?
  3. Print or return?

Follow-ups to expect

  1. "Can you do it with one loop?"
  2. "How do you avoid printing the middle row twice?"
  3. "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