oneby7

Foundations Loops through pattern printing

12. Inverted Star Pyramid

Easy

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

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

That is the pattern for n = 4. The pattern has n rows. The top row holds 2n − 1 stars and starts at the left edge. Each row below starts one space further right and holds two fewer stars, so the bottom row is a single star under the middle of the top row.

The stars in a row sit side by side. A row has no spaces after its last star.

Example 1

Input: n = 4

Output:
*******
 *****
  ***
   *

Explanation: The rows start with 0, 1, 2 and 3 spaces and hold 7, 5, 3 and 1 stars. Nothing comes after the last star.

Example 2

Input: n = 1

Output:
*

Example 3

Input: n = 2

Output:
***
 *

Check your understanding

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

n = 3

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

Article

Send feedback

Thank you for helping. We read every report and fix what is wrong.

How do you feel about oneby7?

Sign in to oneby7

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

New here? This creates your account too.