PPractick

DSA interview question

Pattern 2: Right-Angled Triangle of Stars

Easy · Algorithms · Topics: pattern building

The problem

The second exercise in the pattern series turns the square into a right-angled triangle: the first row has one star, the second has two, and so on until the last row has n. It introduces the single most important idea in pattern problems: the inner loop's bound depends on the row number. Once you see that the number of stars in row i is just i, the change from Pattern 1 is a single character in the code.

Example for n = 4:

*
**
***
****

Function signature

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

Test cases

Input nOutput rowsExplanation
1"*"One row
3"*", "**", "***"Row i has i stars
5"*", "**", "***", "****", "*****"Last row has n stars

(Confirm: rows are left-aligned with no leading spaces; stars have no separators.)

How interviewers phrase it

Clarifying questions to ask

  1. Left-aligned or right-aligned? (Right-aligned needs leading spaces.)
  2. Stars separated by spaces or not?
  3. Print directly or return the rows?

Follow-ups to expect

  1. "What changed from the square?"
  2. "How do you print it right-aligned?"
  3. "How many stars in total?"
  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