Build pyramid and trapezium star patterns as fixed-width rows of strings

Quick Overview

Given a height n, return a centered pyramid and a left-aligned trapezium of asterisks as lists of strings, each row exactly 2n - 1 characters wide. It tests deriving per-row star and space counts from the row index and keeping fixed-width output, trailing spaces included.

Build pyramid and trapezium star patterns as fixed-width rows of strings

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

Given an integer `n`, generate two patterns of height `n`: - A pyramid pattern. Each row `i` (1-indexed) contains `2*i - 1` asterisks (`*`) centered within a total width of `2*n - 1` characters, with spaces padding both sides. - A trapezium pattern. Each row `i` (1-indexed) contains `n + i - 1` asterisks left-aligned within a total width of `2*n - 1` characters, with spaces padding the right side. Return a list containing two lists of strings: the first list is the pyramid pattern and the second is the trapezium pattern. Each string is one row of the pattern, from top to bottom, and has a fixed width of `2*n - 1` characters. ### Function Signature ```python def generate_patterns(n: int) -> list[list[str]]: ``` ### Rules - Rows are returned as strings, not printed. - Every string must be exactly `2*n - 1` characters long, trailing spaces included. Do not strip them. - The only characters allowed are `*` and the space character. ### Constraints - `1 <= n <= 50` ### Examples ```text Input: n = 4 Output: [[" * "," *** "," ***** ","*******"],["**** ","***** ","****** ","*******"]] ``` The width is 7. Drawn out, the pyramid rows are ` * `, ` *** `, ` ***** ` and `*******` (1, 3, 5 and 7 stars centered), and the trapezium rows are `**** `, `***** `, `****** ` and `*******` (4 to 7 stars, left-aligned). ```text Input: n = 1 Output: [["*"],["*"]] ``` With `n = 1` the width is `2*1 - 1 = 1`, so both patterns are a single row containing one asterisk. ```text Input: n = 2 Output: [[" * ","***"],["** ","***"]] ``` The width is 3. The pyramid has 1 star with one space on each side, then 3 stars. The trapezium has 2 stars and one trailing space, then 3 stars.

Overview: Given a height n, return a centered pyramid and a left-aligned trapezium of asterisks as lists of strings, each row exactly 2n - 1 characters wide. It tests deriving per-row star and space counts from the row index and keeping fixed-width output, trailing spaces included.

Nov 25, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Given an integer n, generate two patterns of height n:

  • A pyramid pattern. Each row i (1-indexed) contains 2*i - 1 asterisks ( * ) centered within a total width of 2*n - 1 characters, with spaces padding both sides.
  • A trapezium pattern. Each row i (1-indexed) contains n + i - 1 asterisks left-aligned within a total width of 2*n - 1 characters, with spaces padding the right side.

Return a list containing two lists of strings: the first list is the pyramid pattern and the second is the trapezium pattern. Each string is one row of the pattern, from top to bottom, and has a fixed width of 2*n - 1 characters.

Function Signature

def generate_patterns(n: int) -> list[list[str]]:

Rules

  • Rows are returned as strings, not printed.
  • Every string must be exactly 2*n - 1 characters long, trailing spaces included. Do not strip them.
  • The only characters allowed are * and the space character.

Constraints

  • 1 <= n <= 50

Examples

Input: n = 4
Output: [["   *   ","  ***  "," ***** ","*******"],["****   ","*****  ","****** ","*******"]]

The width is 7. Drawn out, the pyramid rows are * , *** , ***** and ******* (1, 3, 5 and 7 stars centered), and the trapezium rows are **** , ***** , ****** and ******* (4 to 7 stars, left-aligned).

Input: n = 1
Output: [["*"],["*"]]

With n = 1 the width is 2*1 - 1 = 1, so both patterns are a single row containing one asterisk.

Input: n = 2
Output: [[" * ","***"],["** ","***"]]

The width is 3. The pyramid has 1 star with one space on each side, then 3 stars. The trapezium has 2 stars and one trailing space, then 3 stars.