Foundations Loops through pattern printing

16. Number Crown

Easy

Given an integer n, return the pattern below as a list of strings, one string per row, top row first.

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

That is the pattern for n = 4. The crown has n rows. Row i, counting from 1 at the top, has a left half and a right half. The left half counts up from 1 to i and sits against the left edge. The right half counts down from i to 1 and sits against the right edge. Neighbouring numbers in a half are separated by one space.

Every row is as wide as the bottom row, and the gap between the two halves takes up the rest. In the bottom row the gap is a single space. Each row starts and ends with a 1.

Example 1

Input: n = 3

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

Explanation: Every row is 11 characters wide, the width of the bottom row. The gaps are 9, 5 and 1 spaces.

Example 2

Input: n = 1

Output:
1 1

Example 3

Input: n = 5

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

Check your understanding

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

n = 2

Constraints

  • 1 ≤ n ≤ 9
  • Every number is one digit, so the two halves of every row line up in columns.

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.