DSA interview question
Next in the series, the triangle shape stays the same, but instead of stars each row prints the numbers from 1 up to the row number. The shape comes from the same row formula as Pattern 2, and the new part is deciding what to print in each position. The question to ask is whether the printed value depends on the row, the column, or both. Here it depends only on the column.
Example for n = 4:
1
1 2
1 2 3
1 2 3 4
vector<string> numberTriangle(int n); // row i: "1 2 ... i", space-separated, no trailing space
Input n | Output rows | Explanation |
|---|---|---|
| 1 | "1" | One row |
| 3 | "1", "1 2", "1 2 3" | Each row counts from 1 to its row number |
| 5 | "1", "1 2", "1 2 3", "1 2 3 4", "1 2 3 4 5" | |
| 11 | last row "1 2 3 4 5 6 7 8 9 10 11" | Space separators keep two-digit numbers readable |
(Confirm: numbers are separated by single spaces with no trailing space; some judges print them without spaces, which only changes the separator.)
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