PPractick

DSA interview question

Pattern 6: Inverted Triangle of Numbers

Easy · Algorithms · Topics: pattern building

The problem

This pattern combines the shrinking shape of Pattern 5 with the counting rows of Pattern 3: the first row prints 1 2 ... n, and each following row stops one number earlier, until the last row prints just 1. It checks whether you can combine two ideas you've already used: the row length comes from the row number (n − i + 1), while the printed value comes from the column (j).

Example for n = 4:

1 2 3 4
1 2 3
1 2
1

Function signature

vector<string> invertedNumberTriangle(int n);  // row i: "1 2 ... (n - i + 1)"

Test cases

Input nOutput rowsExplanation
1"1"One row
3"1 2 3", "1 2", "1"Each row counts up from 1 and ends one earlier
5"1 2 3 4 5", "1 2 3 4", "1 2 3", "1 2", "1"

(Confirm: numbers are space-separated with no trailing space; some judges print them without spaces.)

How interviewers phrase it

Clarifying questions to ask

  1. Space-separated numbers or not?
  2. Trailing spaces allowed?
  3. Print or return?

Follow-ups to expect

  1. "Which loop controls the length and which controls the value?"
  2. "How would you print n n−1 ... 1 in each row instead?"
  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