PPractick

DSA interview question

Pattern 21: Hollow Square

Medium · Algorithms · Topics: pattern building

The problem

The hollow square draws only the border of an n × n square: the first and last rows are solid, and every row in between has a star at each end with spaces inside. It's the first pattern where the character at a position depends on a condition (is this cell on the border?) rather than a simple count, which is exactly how grid problems check boundaries later on.

Example for n = 4:

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

Function signature

vector<string> hollowSquare(int n);  // n rows of n characters; stars on the border only

Test cases

Input nOutput rowsExplanation
1"*"A single cell is all border
2"**", "**"Every cell is on the border
3"***", "* *", "***"One interior cell
4"****", "* *", "* *", "****"A 2 × 2 interior

(Confirm: the pattern is an n × n square; interior rows end with a star, so there are no trailing spaces.)

How interviewers phrase it

Clarifying questions to ask

  1. Square (n × n) or rectangle (r × c)?
  2. Stars separated by spaces?
  3. What should n = 1 and n = 2 look like?

Follow-ups to expect

  1. "What's the condition for a border cell?"
  2. "How would you do a hollow rectangle of r rows and c columns?"
  3. "How would you draw the diagonals too?"
  4. "Complexity?"

Practise questions like this with a real engineer

On Practick another engineer interviews you live for 60 minutes on a question from this bank. They have the full interviewer guide beside the call: the expected answer to every follow-up, hints to give if you stall, and a scoring rubric. You get the model solution once the session settles. It is free: interview someone for an hour and you have earned your own.

Book a free mock interview

Related questions

All 474 dsa interview questions

DSA mock interviews · System design interview questions · Free mock interview practice · Browse interviewers · Sign in