Foundations Loops through pattern printing

26. Concentric Number Squares

Medium

Given an integer n, return the pattern below as a list of rows, top row first. Each row is a list of the numbers in it, from left to right.

4 4 4 4 4 4 4
4 3 3 3 3 3 4
4 3 2 2 2 3 4
4 3 2 1 2 3 4
4 3 2 2 2 3 4
4 3 3 3 3 3 4
4 4 4 4 4 4 4

That is the pattern for n = 4. It is a square made of rings, one inside the other. The outer ring is all n, the ring inside it is all n − 1, and so on, down to a single 1 in the centre. The square has 2n − 1 rows, and every row holds 2n − 1 numbers. For n = 4 the answer is [[4,4,4,4,4,4,4],[4,3,3,3,3,3,4],[4,3,2,2,2,3,4],[4,3,2,1,2,3,4],[4,3,2,2,2,3,4],[4,3,3,3,3,3,4],[4,4,4,4,4,4,4]].

Example 1

Input: n = 4

Output:
4 4 4 4 4 4 4
4 3 3 3 3 3 4
4 3 2 2 2 3 4
4 3 2 1 2 3 4
4 3 2 2 2 3 4
4 3 3 3 3 3 4
4 4 4 4 4 4 4

Example 2

Input: n = 1

Output:
1

Example 3

Input: n = 6

Output:
6 6 6 6 6 6 6 6 6 6 6
6 5 5 5 5 5 5 5 5 5 6
6 5 4 4 4 4 4 4 4 5 6
6 5 4 3 3 3 3 3 4 5 6
6 5 4 3 2 2 2 3 4 5 6
6 5 4 3 2 1 2 3 4 5 6
6 5 4 3 2 2 2 3 4 5 6
6 5 4 3 3 3 3 3 4 5 6
6 5 4 4 4 4 4 4 4 5 6
6 5 5 5 5 5 5 5 5 5 6
6 6 6 6 6 6 6 6 6 6 6

Check your understanding

Work out the answer for this input before you write any code.

n = 2

Constraints

  • 1 ≤ n ≤ 100

Clarifying questions

What you would ask an interviewer before writing any code.

Hints

Interview follow-ups

Runs in your browser
Code
Runs in your browser for now · some things may not work as expectedLn 1, Col 1

Sign in to oneby7

Keep your progress, your streak and your code, on any device.

New here? This creates your account too.