PPractick

DSA interview question

Pattern 1: Square of Stars

Easy · Algorithms · Topics: pattern building

The problem

You're starting a series of pattern exercises, and the first one is the simplest possible shape: an n × n square of stars. It's the base every other pattern builds on. The outer loop controls the rows, the inner loop controls what goes in each row, and here every row is identical. Getting this one right (with the right number of rows and columns, and nothing extra at the ends) sets up the mental model for the next twenty patterns.

Example for n = 3:

***
***
***

Function signature

vector<string> squarePattern(int n);  // n rows, each with n stars

Test cases

Input nOutput rowsExplanation
1"*"One row, one star
3"***", "***", "***"Three identical rows
5five rows of "*****"Rows and columns both equal n

(Confirm: stars are printed without separators; some judges, such as GeeksforGeeks, expect a space after each star, which only changes what is appended in the inner loop.)

How interviewers phrase it

Clarifying questions to ask

  1. Should the stars be separated by spaces?
  2. Print directly, or return the rows?
  3. What should happen for N = 0?

Follow-ups to expect

  1. "What does each loop control?"
  2. "How would you print a rectangle of R rows and C columns?"
  3. "How would you make it hollow?"
  4. "What's the 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