Skip to content
CodeKruPro

Build a star pyramid pattern

Coding problemEasyAsked in: Accenture, Capgemini, Cognizant

Given a number of rows n, build a centred pyramid of stars and return its rows from top to bottom, one text line per row.

Row i (counting from 1) has n - i spaces followed by 2i - 1 stars (the * character), and nothing after the stars. For n = 0 return an empty list.

Examples

Example 1
Input: n = 3
Output: [" *"," ***","*****"]
Example 2
Input: n = 1
Output: ["*"]
Example 3
Input: n = 0
Output: []

Constraints

  • n is between 0 and 200

Run checks the 3 examples. Submit also runs the hidden tests: edge cases, large inputs and a time limit.

Run your code to see the results here.

This question is part of Pro+

Pro+ opens every question in Interview Prep and the mock interview. Every article and the first 2 questions of each topic stay free. Hands-on Selenium and API tasks follow their course lesson's own lock.

See Pro+

Already have Pro+? Sign in