Animate the Hollow Square pattern and see how a single border condition selects exactly the right cells.
About this algorithm
The hollow square prints a square outline: full rows of stars on the top and bottom, and a star at each end of every row in between, with blanks inside. Unlike the triangles, the decision has nothing to do with counting stars per row. A cell gets a star only if it sits on the border.
Use this page to learn the second kind of pattern rule, a test on where a cell is rather than how many cells come before it. The triangles and pyramids all ask "is j inside this row's range". Here the question is "is this cell on an edge", which is the same check you write for a picture frame, a game board boundary or the outer ring of a matrix. Once it clicks, hollow triangles and hollow pyramids are the same idea applied to their own edges.
Two loops, i for rows and j for columns, both from 0 to n - 1. Every cell of the n by n square is checked once.
The top edge is row 0, the bottom edge is row n - 1, the left edge is column 0 and the right edge is column n - 1.
Print a star if i == 0 or i == n - 1 or j == 0 or j == n - 1. If none of the four is true, the cell is inside and gets a space.
End each row with a newline. The first and last rows come out solid; every middle row comes out as a star, n - 2 spaces, and a star.
With n = 5 the page draws a 5 by 5 square. Row 0 is all edge, so *****. Rows 1, 2 and 3 have a star at column 0, three spaces at columns 1 to 3, and a star at column 4: * *. Row 4 is solid again. That makes 5 + 2 + 2 + 2 + 5 = 16 stars, which matches the formula 4n - 4 (four sides of n, minus the four corners counted twice). The hollow middle is (n - 2)² = 9 spaces, and 16 + 9 = 25 cells.
At n = 2 every cell is on an edge, so the square comes out solid, and at n = 1 it is a single star. The slider here starts at 3, the first size with a hole in the middle.
For a hollow pyramid, keep the pyramid's range and print a star only at its two ends or on the last row. The border test changes shape, but the pattern of thinking is the same.
Time: all n² cells are tested, including the empty inside, because the loop has to print those spaces too. Space: the edge test needs only i, j and n, so nothing grows with the size of the square.