# The Inverted Triangle

> A pattern of stars narrows toward the bottom.

Canonical URL: <https://datadriven.io/problems/the_inverted_triangle>

Domain: Python · Difficulty: medium · Seniority: L3

## Problem

Given positive integer n, return a list of n strings. Row 0 has n asterisks, row 1 has n-1, ..., row n-1 has 1 asterisk.

## Worked solution and explanation

### Why this problem exists in real interviews

This tests **string construction with decreasing iteration**, a basic loop control problem. It probes whether a candidate can generate patterns using arithmetic sequences.

---

### Break down the requirements

#### Step 1: Loop from n down to 1

Each iteration produces one row with that many asterisks.

#### Step 2: Build each row string

Repeat `'*'` the appropriate number of times.

#### Step 3: Return as a list of strings

One string per row.

---

### The solution

**Decreasing row construction**

```python
def inverted_triangle(n: int) -> list:
    rows = []
    for i in range(n, 0, -1):
        row = ""
        for _ in range(i):
            row += "*"
        rows.append(row)
    return rows
```

> **Time and Space Complexity**
>
> **Time:** O(n^2) total characters across all rows (arithmetic sum).
> 
> **Space:** O(n^2) for the output strings.

> **Interviewers Watch For**
>
> Whether you use `range(n, 0, -1)` or equivalent. Some candidates use `range(n)` and compute `n - i` asterisks, which is correct but less readable.

> **Common Pitfall**
>
> Adding leading spaces for center alignment when the problem only asks for left-aligned asterisks.

---

## Common follow-up questions

- How would you center-align the triangle? _(Tests adding leading spaces: `' ' * (n - i)` before the asterisks.)_
- How would you print a diamond shape? _(Tests combining an upward and inverted triangle.)_
- What if you needed to return a single string with newlines? _(Tests joining the list with `'\n'.join(rows)`.)_

## Related

- [All practice problems](https://datadriven.io/problems)
- [Mock interview mode](https://datadriven.io/interview/the_inverted_triangle)
- [Python Interview Questions](https://datadriven.io/python-interview-questions)
- [Data Engineering Interview Prep Guide](https://datadriven.io/data-engineer-interview-prep)
- [Daily Challenge](https://datadriven.io/daily)

---

Source: DataDriven (https://datadriven.io). 100% free data engineering interview prep. Live code execution against Postgres 16, Python 3.11, and Spark sandboxes. No paywall, no premium tier, no signup gate.