PPractick

DSA interview question

Pattern 13: Increasing Number Triangle (Floyd's Triangle)

Easy · Algorithms · Topics: pattern building

The problem

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

Function signature

vector<string> floydTriangle(int n);  // row i has i consecutive numbers continuing from the previous row

Test cases

Input nOutput rowsExplanation
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
5last row "11 12 13 14 15"Two-digit numbers, still space-separated

(Confirm: space-separated with no trailing space.)

How interviewers phrase it

Clarifying questions to ask

  1. Space-separated?
  2. How large can N be? (The last number is n(n + 1) / 2.)
  3. Print or return?

Follow-ups to expect

  1. "Why declare the counter outside the loops?"
  2. "What's the first number of row i without a counter?"
  3. "What's the last number printed?"
  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