DSA interview question
In this triangle the numbers keep counting across rows: row 1 has 1, row 2 has 2 3, row 3 has 4 5 6, and so on. It's known as Floyd's triangle. Unlike Patterns 3 and 4, the printed value depends on neither the row nor the column alone, but on how many numbers have been printed so far. The natural solution is a counter that lives outside both loops.
Example for n = 4:
1
2 3
4 5 6
7 8 9 10
vector<string> floydTriangle(int n); // row i has i consecutive numbers continuing from the previous row
Input n | Output rows | Explanation |
|---|---|---|
| 1 | "1" | |
| 3 | "1", "2 3", "4 5 6" | The counter carries over between rows |
| 4 | "1", "2 3", "4 5 6", "7 8 9 10" | Last number is n(n + 1) / 2 = 10 |
| 5 | last row "11 12 13 14 15" | Two-digit numbers, still space-separated |
(Confirm: space-separated with no trailing space.)
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