Foundations Loops through pattern printing

25. Hollow Square

Medium

Given an integer n, return the pattern below as a list of strings, one string per row, top row first.

*****
*   *
*   *
*   *
*****

That is the pattern for n = 5. The square has n rows, and every row is n characters wide. The cells on the border are stars (*): the first row, the last row, the first column and the last column. Every other cell is a space.

Example 1

Input: n = 4

Output:
****
*  *
*  *
****

Explanation: The two middle rows have a star at each end and 2 spaces inside.

Example 2

Input: n = 1

Output:
*

Example 3

Input: n = 2

Output:
**
**

Explanation: Every cell of a 2 × 2 square is on the border.

Check your understanding

Work out the answer for this input before you write any code.

n = 3

Constraints

  • 1 ≤ n ≤ 100

Clarifying questions

What you would ask an interviewer before writing any code.

Hints

Interview follow-ups

Runs in your browser
Code
Runs in your browser for now · some things may not work as expectedLn 1, Col 1

Sign in to oneby7

Keep your progress, your streak and your code, on any device.

New here? This creates your account too.