PPractick

DSA interview question

Pattern 5: Inverted Right-Angled Triangle of Stars

Easy · Algorithms · Topics: pattern building

The problem

After building a triangle that grows, the next exercise flips it: the first row has n stars and each following row has one fewer, down to a single star. It's the same shape as Pattern 2 upside down, and it teaches you to express a shrinking row length as a formula, n − i + 1 for row i. Getting that formula right, rather than guessing, is what stops the classic "one row too many or too few" bug.

Example for n = 4:

****
***
**
*

Function signature

vector<string> invertedTriangle(int n);  // row i (1-indexed) has n - i + 1 stars

Test cases

Input nOutput rowsExplanation
1"*"One row
3"***", "**", "*"Starts at n, shrinks by one
5"*****", "****", "***", "**", "*"Last row has exactly one star

(Confirm: rows are left-aligned, stars have no separators, and there are exactly n rows.)

How interviewers phrase it

Clarifying questions to ask

  1. Left-aligned or right-aligned?
  2. Stars separated by spaces?
  3. Print or return the rows?

Follow-ups to expect

  1. "How did you get n − i + 1?"
  2. "Is there another way to write it?"
  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