DSA interview question
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:
****
***
**
*
vector<string> invertedTriangle(int n); // row i (1-indexed) has n - i + 1 stars
Input n | Output rows | Explanation |
|---|---|---|
| 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.)
n − i + 1?"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