Last Updated: June 7, 2026
A hollow square is an n × n grid where only the outline is marked and the interior is left blank.
Number the rows 0 to n - 1 and the columns 0 to n - 1. A position belongs to the frame when it is in the first or last row, or in the first or last column. The first and last rows are solid lines of stars. The rows in between show a star at each end and spaces in the middle.
For n = 4, the answer is four rows. The top and bottom are ****, and the two middle rows are * *, a star, two spaces, then a star. Each of those middle rows still has a length of 4, because the spaces count as real characters in the string.
1 <= n <= 100 → n is always at least 1, so you never return an empty list. The sizes stay small, so building each row character by character is fast enough.n → The interior spaces are part of the output, not something to strip away. A middle row of a square with n = 5 is * *, which is five characters wide, not just the two stars.n is 1 or 2 there is no interior, so every cell lies on the border. The table of n = 1 is a single *, and n = 2 is two rows of **.The shape is a rule about each cell, so the most direct method is to walk every cell and apply it. Go row by row, and within each row column by column. At each position, check whether the cell is on the border: if it is, write a *, otherwise write a space.
A cell is on the border when row == 0, row == n - 1, col == 0, or col == n - 1. If any one of those holds, the cell is part of the frame.
row from 0 to n - 1.col from 0 to n - 1.row or col is at an edge of the grid, append a *, otherwise append a space.Input:
With n = 4, the loop builds four rows. For row = 0, every column satisfies row == 0, so all four cells become stars and the row is ****. For row = 1, only col == 0 and col == 3 are on the border, while col == 1 and col == 2 are interior, so the row is * *. Row 2 follows the same pattern and is also * *. For row = 3, the condition row == n - 1 holds for every column, so the row is again ****. Collecting the rows gives the framed square:
n × n cells once, doing constant work per cell.n strings of length n, so the total characters stored grow with the area of the square.Checking every cell works, but most rows of the square follow one of only two shapes. Can we build those shapes once and reuse them instead of testing each cell?
The square has only two distinct row shapes. The top and bottom rows are a solid line of n stars. Every row in between is a star, then n - 2 spaces, then a star.
So build those two strings once instead of deciding each cell. Make a full row of n stars and a middle row of a star, n - 2 spaces, then a star. Assemble the answer with full at the top, full at the bottom, and middle for each row in between.
When n is 1 or 2, there are no interior rows, so the square is made entirely of full rows and the middle row is never used.
full string of n stars.n is 1, return a list containing just that single full row.middle string of a star, then n - 2 spaces, then a star.full as the first row.middle once for each of the n - 2 interior rows.full as the last row, then return the list.Input:
With n = 4, the full row is **** and the middle row is a star, two spaces, then a star, which is * *. The square has n - 2 = 2 interior rows. Assembly puts full first, then two copies of middle, then full last. That yields ****, * *, * *, ****, the same square as before but built from two reusable pieces:
full and middle strings costs O(n) each, and adding middle for the n - 2 interior rows produces O(n) characters per row, so the total work scales with the area.n rows of length n. The reused full and middle strings add only O(n) beyond the output.