AlgoInsight

DSA problem

Hollow Square of Stars

EasyPattern PrintingPattern: Conditional PrintingbeginnerFree

Given an integer N, print a hollow square of stars with N rows and N columns. Only the border carries stars; the interior is filled with spaces.

This is your introduction to conditional printing inside a loop. Instead of always printing a star, you ask a question at each cell: am I on the first row, last row, first column, or last column? If yes, print *; otherwise print a space.

That if-inside-a-loop pattern shows up everywhere in real code - rendering borders, walking the edges of a matrix, handling boundary conditions. It teaches you to reason about when to print, not just what to print.

Examples

Example 1
Input: N = 5
Output: ***** * * * * * * *****
Explanation: Cells on the top row, bottom row, left column, or right column are stars; the rest are spaces.

Constraints

3 hints and a full solution walkthrough with step-by-step visualizations are available in the practice editor.

patternloops

Browse all DSA problems