AlgoMaster Logo

Hourglass Pattern

Last Updated: June 7, 2026

medium
5 min read

Understanding the Problem

The hourglass is two pyramids joined at their tips. The top is an inverted pyramid, wide at the top and narrowing to a single star. The bottom is a normal pyramid, starting from a single star and widening back out. Where they meet, in the middle row, the pattern pinches down to one star, the waist of the shape.

Each row needs two counts lined up: the leading spaces that push the stars toward the center, and the stars themselves. Wrong spaces and the stars no longer center; wrong star count and the edges look ragged.

For a top row numbered i from 1 to n, the row begins with i - 1 spaces, so the first row has none and each row below is indented one more. After the spaces come 2 * (n - i) + 1 stars, widest at the top and shrinking by two each row until a single star at i = n. There are no trailing spaces. The bottom half then replays rows n - 1 down to 1, mirroring the top so the lower pyramid opens back up.

Key Constraints:

  • 1 <= n <= 100n is always at least 1. The smallest case, n = 1, gives a single row *, which is the pinch point with no pyramid above or below it.
  • Star counts are always odd → Each row holds 2 * (n - i) + 1 stars, which is always an odd number. That odd count is what lets the stars sit symmetrically around a center column.
  • No trailing spaces → Rows have leading spaces only. The widest rows at the top and bottom have no spaces at all, while the middle row has n - 1 leading spaces before its single star.

Approach 1: Two Halves

Intuition

The hourglass divides into a top inverted pyramid and a bottom upright pyramid. Handle each half with its own loop, sharing one rule for building a row from a value i: i - 1 spaces followed by 2 * (n - i) + 1 stars.

The top loop walks i upward from 1 to n, so the indentation increases and the star count shrinks down to a single star. The bottom loop walks i back down from n - 1 to 1, so the indentation decreases and the stars widen again.

Algorithm

  1. Create an empty list to hold the rows.
  2. For i from 1 up to n, build a row with i - 1 leading spaces followed by 2 * (n - i) + 1 stars, and add it to the list.
  3. For i from n - 1 down to 1, build the same kind of row and add it to the list.
  4. Return the list of rows.

Example Walkthrough

Input:

3
n

The top loop runs for i = 1, 2, 3. At i = 1, the row is 0 spaces and 2 * (3 - 1) + 1 = 5 stars, giving *****. At i = 2, it is 1 space and 2 * (3 - 2) + 1 = 3 stars, giving ***. At i = 3, it is 2 spaces and a single star, giving *, the pinch point. The bottom loop then runs for i = 2, 1, producing *** and ***** again. The rows stack into the full hourglass:

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

Code

Two loops keep the row rule readable, but they repeat the same logic in both directions. The whole hourglass can be drawn in one pass if we describe each row by its distance from the pinched middle.

Approach 2: Distance From the Middle

Intuition

The hourglass is symmetric around its middle row, the single-star pinch point. A row's width depends only on how far it sits from that middle, not on whether it is above or below.

Number all 2n - 1 rows with a single index and measure the distance from the middle. At the middle the distance is 0, so there is one star and n - 1 leading spaces. As the distance grows toward n - 1 at the edges, the spaces drop to 0 and the stars grow. The leading spaces equal (n - 1) - distance, and the star count equals 2 * distance + 1. One loop over all the rows is enough.

Algorithm

  1. Create an empty list to hold the rows.
  2. Loop a row index r from 0 to 2n - 2, covering every row.
  3. Compute the distance from the middle as the absolute value of r - (n - 1).
  4. Set the leading spaces to (n - 1) - distance and the star count to 2 * distance + 1.
  5. Build a row with that many spaces followed by that many stars, and add it to the list.
  6. Return the list of rows.

Example Walkthrough

Input:

3
n

With n = 3, the middle row index is n - 1 = 2, and there are 2n - 1 = 5 rows. At r = 0, the distance is |0 - 2| = 2, so the row has (3 - 1) - 2 = 0 spaces and 2 * 2 + 1 = 5 stars, giving *****. At r = 1, the distance is 1, so 1 space and 3 stars, giving ***. At r = 2, the distance is 0, so 2 spaces and 1 star, giving *, the pinch. At r = 3, the distance climbs back to 1, giving ***. At r = 4, the distance is 2, giving *****. The single loop produces the same five rows:

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

Code