Easy 3 solved 100.0% acceptance

Hollow Square

Draw the outline of a square with * characters.

What you'll learn

  • Draw a border with characters
  • Detect first and last rows and columns
  • Build strings row by row

Problem

Draw a hollow square of side size: the first and last rows are all *, and the rows in between have * only at the two ends with spaces inside.

Implement hollow_square(size).

Input

size: The side length.

Output

Return the rows from top to bottom.

Constraints

1 ≤ size ≤ 50

Examples

Example 1
Input:
size = 4

Expected output:
expected = ["****", "*  *", "*  *", "****"]
Input:
long long size = 4;

Expected output:
std::vector<std::string> expected = {"****", "*  *", "*  *", "****"};
Input:
long size = 4;

Expected output:
string[] expected = new string[] {"****", "*  *", "*  *", "****"};

Explanation

Rows are "****", "* *", "* *", "****".

Example 2
Input:
size = 1

Expected output:
expected = ["*"]
Input:
long long size = 1;

Expected output:
std::vector<std::string> expected = {"*"};
Input:
long size = 1;

Expected output:
string[] expected = new string[] {"*"};

Explanation

A 1×1 square is a single "*".

How to approach it

Try on your own first, then open one hint at a time.

Hint 1

The border is filled, while the inside stays empty.

Hint 2

Treat the first and last rows differently from the middle rows.

Hint 3

For size one, the single cell is both border and corner, so it must be filled.

Expected complexity

O(size^2) time, O(size^2) extra space

References

This challenge is adapted for CodeUtility. Follow the statement and constraints above.

Learn from others

Community solutions

Spoiler alert: 3 full solutions below Try solving it yourself first. You can still peek at other developers' code and AI reviews. Sign in and solve the challenge to join the discussion.