AlgoMaster Logo

Hollow Triangle

Last Updated: June 7, 2026

easy
5 min read

Understanding the Problem

A hollow triangle keeps only the outline of a left-aligned right triangle. Each row starts at the left margin and grows one character wider than the row above it. Row 1 has one character, row 2 has two, and row n has n.

Three parts keep their stars: the left vertical edge, which is the first column of every row, the slanted right edge, which is the last position of each row, and the bottom edge, which is the entire final row. Anything off these three edges becomes a space.

For n = 5, look at row 3. It has three positions. The first column is an edge, the third position is the slanted edge for that row, and the middle is interior. So row 3 is * *, a star, a space, then a star. The final row, row 5, is the base, so all five positions are stars and it reads *****.

Key Constraints:

  • 1 <= n <= 100n is always at least 1, so there is always a first row. The sizes are small, so building rows one character at a time is fast enough.
  • Rows have different lengths → Unlike a square, the rows here are not all the same width. Row i has exactly i characters, and the output is not padded to a common width.
  • Short triangles look solid → For the first couple of rows, every position is also an edge, so they come out completely filled. Row 1 is * and row 2 is **, with no interior to hollow out.

Approach 1: Boundary Conditions

Intuition

The shape is a rule about each position, so the most direct method is to visit every position and apply it. Walk the rows from 1 to n, and within each row walk the columns from 0 to i - 1.

A position is on an edge when j == 0, j == i - 1, or i == n. If any of those holds, write a *. Otherwise the position is interior, so write a space.

Algorithm

  1. Create an empty list to hold the rows.
  2. Loop i from 1 to n, where i is both the row number and its length.
  3. For each row, build a string by looping j from 0 to i - 1.
  4. If j == 0, j == i - 1, or i == n, append a *, otherwise append a space.
  5. Add the finished row string to the list.
  6. After all rows are built, return the list.

Example Walkthrough

Input:

4
n

With n = 4, the loop builds four rows. Row 1 has one position at j = 0, which satisfies j == 0, so it is *. Row 2 has positions j = 0 and j = 1. Both are edges since one is the first column and the other is the last, so the row is **. Row 3 has positions 0, 1, 2. Here j = 0 is the first column and j = 2 is the last, but j = 1 is interior and i is not yet the bottom row, so the row is * *. Row 4 is the bottom row, so i == n holds for every position and the row is the solid base ****. Together they form the hollow triangle:

0
*
1
**
2
* *
3
****
result

Code

Testing every position works, but the stars sit in a small number of predictable places. Can we place those stars directly instead of checking each position?

Approach 2: Edges Then Base

Intuition

Instead of deciding every position, place the stars directly. Any row that is not the bottom row has only two filled positions, the first column and the last, so it is a star, a run of spaces, then a star. The bottom row is the solid base, every position a star.

To build a row above the base, start with i spaces, then set the first and last characters to stars. Rows of length 1 and 2 have no gap between their two ends, so they fill up on their own. The bottom row skips the gap and is just i stars.

Algorithm

  1. Create an empty list to hold the rows.
  2. Loop i from 1 to n.
  3. If i == n, this is the base, so add a row of i stars and continue.
  4. Otherwise start a row of i spaces.
  5. Set the first character (index 0) to a star.
  6. Set the last character (index i - 1) to a star. For i == 1 this is the same position, which stays a single star.
  7. Add the row to the list, then return it after the loop.

Example Walkthrough

Input:

4
n

With n = 4, rows 1 through 3 are above the base. Row 1 starts as one space, then index 0 and the last index both point to position 0, leaving a single *. Row 2 starts as two spaces, and setting index 0 and index 1 to stars fills it to **. Row 3 starts as three spaces, and setting index 0 and index 2 to stars gives * *, with the middle space untouched. Row 4 is the base, so it is added as four stars, ****. The assembled rows match the hollow triangle:

0
*
1
**
2
* *
3
****
result

Code