AlgoMaster Logo

Number Pyramid

Last Updated: June 7, 2026

easy
3 min read

Understanding the Problem

A number pyramid is a stack of rows where each row counts one further than the one above it. Row 1 is 1, row 2 counts 1 and 2 to give 12, row 3 counts up to 3 to give 123. Each step down adds the next digit to the end.

The output is a list of strings, one per row. Row i is the digits from 1 up to i with nothing in between. For n = 3 the answer is ["1", "12", "123"]. For n = 1 it is ["1"].

Because the digits sit directly next to each other, n is capped so every digit stays a single character. That keeps "123" meaning the digits 1, 2, 3 rather than a larger number.

Key Constraints:

  • 1 <= n <= 9n is always at least 1, so there is always at least one row. The cap at 9 keeps each digit a single character, so a row never mixes single and multi-digit numbers.
  • Row i holds the digits 1 through i → The length of row i is exactly i characters. The last row has n characters, and the rows grow by one character each step down.

Approach 1: Nested Loops

Intuition

Each row is built by counting from 1 up to the row's number, which is a loop inside a loop. The outer loop walks the rows from 1 to n, and for each row the inner loop counts from 1 up to that row's index, appending every digit.

The outer index i does double duty: it names the row and sets how far the inner loop counts. Row i counts up to i, so the inner loop runs while its counter is at most i.

Algorithm

  1. Create an empty list result to hold the rows.
  2. For each row index i from 1 to n:
    • Start with an empty string for the row.
    • For each j from 1 to i, append the digit j to the string.
    • Add the finished string to result.
  3. Return result.

Example Walkthrough

Input:

4
n

For row 1 the inner loop runs only with j = 1, producing "1". For row 2 it runs j = 1 then j = 2, producing "12". For row 3 it counts through 1, 2, 3 to give "123". For row 4 it counts to 4, giving "1234". Each row's string is collected in turn:

0
1
1
12
2
123
3
1234
result

Code

The inner loop rebuilds the full count from 1 for every single row, even though each row only adds one digit to the row above it. Can we reuse the previous row instead of counting from the start each time?

Approach 2: Extend the Previous Row

Intuition

Row 3 is "123" and row 4 is "1234", differing only by the extra 4. Every row is the row above it with one more digit appended.

So keep the text of the current row, and on each step append the next digit before recording it. There is no need to recount from 1, because the running string already holds everything up to the previous digit.

Algorithm

  1. Create an empty list result and an empty string current.
  2. For each i from 1 to n:
    • Append the digit i to current.
    • Add current to result as the row for this step.
  3. Return result.

Example Walkthrough

Input:

4
n

The running string starts empty. On step 1, appending 1 makes it "1", which becomes row 1. On step 2, appending 2 makes it "12", row 2. Step 3 appends 3 to give "123", and step 4 appends 4 to give "1234". Each row is recorded right after its digit is added:

0
1
1
12
2
123
3
1234
result

Code