AlgoMaster Logo

Hollow Square

Last Updated: June 7, 2026

easy
4 min read

Understanding the Problem

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.

Key Constraints:

  • 1 <= n <= 100n 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.
  • Each row has length 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.
  • Small values collapse the shape → When 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 **.

Approach 1: Border Condition

Intuition

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.

Algorithm

  1. Create an empty list to hold the rows.
  2. Loop row from 0 to n - 1.
  3. For each row, build a string by looping col from 0 to n - 1.
  4. If row or col is at an edge of the grid, 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. 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:

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

Code

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?

Approach 2: Compose Edge and Middle Rows

Intuition

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.

Algorithm

  1. Build a full string of n stars.
  2. If n is 1, return a list containing just that single full row.
  3. Build a middle string of a star, then n - 2 spaces, then a star.
  4. Add full as the first row.
  5. Add middle once for each of the n - 2 interior rows.
  6. Add full as the last row, then return the list.

Example Walkthrough

Input:

4
n

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:

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

Code