AlgoInsight

DSA problem

Hollow Pyramid of Stars

MediumPattern PrintingPattern: Conditional PrintingbeginnerFree

Given an integer N, print a hollow pyramid of stars with N rows. Only the two slanted edges and the base row carry stars; the inside is hollow.

This combines the centering logic of a solid pyramid with the conditional printing of a hollow shape. For each row you print N - i leading spaces, then decide: the first row is a single star, the last row is a full base of 2 * N - 1 stars, and every row in between is a star, some interior spaces, and another star.

The key skill is recognizing which positions are border positions inside a triangular shape and handling several conditions at once.

Examples

Example 1
Input: N = 5
Output: * * * * * * * *********
Explanation: Row 1 is a single star, the base is solid, and middle rows show only their two edge stars.

Constraints

3 hints and a full solution walkthrough with step-by-step visualizations are available in the practice editor.

patternloops

Browse all DSA problems