FastPrepBuild a Hollow Square

Build a Hollow Square

Visa logoVisa● EasyINTERNOA
Learn

Problem statement

You are given an integer n. Build an n by n hollow square as a string array.

  • Every border position contains *.
  • Every interior position contains -.

Return the rows from top to bottom.

Function

buildHollowSquare(n: int) → String[]

Examples

Example 1

n = 4return = ["****","*--*","*--*","****"]

The first and last rows are all border characters. Each middle row has two border stars surrounding two interior dashes.

Example 2

n = 2return = ["**","**"]

A square of side two has no interior positions, so both rows contain only stars.

Constraints

  • 2 <= n <= 1000
  • The returned output contains exactly n2 characters.

More Visa problems

See Visa hiring insights
public String[] buildHollowSquare(int n) {
    // Write your code here.
}
n4
expected["****", "*--*", "*--*", "****"]
Checking account…