Easy 3 solved 100.0% acceptance

Pyramid Rows

Draw a right-aligned pyramid of # blocks.

What you'll learn

  • Build rows with padding and blocks
  • Compute spaces and symbols per row
  • Return lines as a list

Problem

Draw a right-aligned pyramid like the one in Super Mario: row i (counting from 1) has height − i spaces followed by i # characters.

Implement pyramid_rows(height).

Input

height: The number of rows.

Output

Return the rows from top to bottom.

Constraints

0 ≤ height ≤ 50

Examples

Example 1
Input:
height = 3

Expected output:
expected = ["  #", " ##", "###"]
Input:
long long height = 3;

Expected output:
std::vector<std::string> expected = {"  #", " ##", "###"};
Input:
long height = 3;

Expected output:
string[] expected = new string[] {"  #", " ##", "###"};

Explanation

Rows are " #", " ##" and "###": each row has one more block and one less space.

Example 2
Input:
height = 1

Expected output:
expected = ["#"]
Input:
long long height = 1;

Expected output:
std::vector<std::string> expected = {"#"};
Input:
long height = 1;

Expected output:
string[] expected = new string[] {"#"};

Explanation

A single row with one block: "#".

How to approach it

Try on your own first, then open one hint at a time.

Hint 1

Each row combines leading spaces with a growing run of hash characters.

Hint 2

Build rows one by one from top to bottom so the alignment stays correct.

Hint 3

If the height is zero, return no rows rather than an empty-looking pyramid.

Expected complexity

O(height^2) time, O(height^2) extra space

References

This challenge is adapted for CodeUtility. Follow the statement and constraints above.

Learn from others

Community solutions

Spoiler alert: 3 full solutions below Try solving it yourself first. You can still peek at other developers' code and AI reviews. Sign in and solve the challenge to join the discussion.