# Top-Heavy

> The widest row leads, and everything below it narrows.

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

Domain: Python · Difficulty: medium · Seniority: L3

## Problem

A console report renders a quick text gauge from a single count: given a positive integer `n`, return `n` lines of asterisks where the first line is the widest with `n` of them and every line below loses one, ending at a single asterisk.

## Worked solution and explanation

### What this is really testing

Strip the costume and this is a row of widths counting down: n, n-1, ..., 1, each rendered as that many asterisks. Everyone gets the rows right; the only place candidates trip is the direction. The first row is the WIDEST, so you are counting DOWN, not up, and the off-by-one (starting at n, stopping at 1) is where a rushed answer produces n+1 rows or an empty last row.

---

### Break down the requirements

#### Step 1: Iterate widths high to low

Walk the widths from n down to 1. Each value is exactly the asterisk count for that row, so you never compute n minus anything.

#### Step 2: Turn each width into a row

A width w becomes the string of w asterisks directly. String repetition does the work in one expression.

#### Step 3: Return the rows

Collect the rows in order into a list of strings. The first element is the widest, the last is a single asterisk.

---

### The solution

**Decreasing widths, one pass**

```python
def inverted_triangle(n: int) -> list[str]:
    return ["*" * width for width in range(n, 0, -1)]
```

> **Trick to solving**
>
> range(n, 0, -1) yields n, n-1, ..., 1 and naturally excludes 0, so the narrowest row is a single asterisk and never an empty string. Counting down from n means each loop value IS the width, with no n - i arithmetic to get backwards.

> **Time and Space Complexity**
>
> Time is O(n^2): the rows hold n + (n-1) + ... + 1 characters total, an arithmetic sum. Space is the same, since that is the size of the output you must return. There is no cheaper option; you are emitting O(n^2) characters by definition.

> **Interviewers Watch For**
>
> The clean down-count with range(n, 0, -1). An up-count with range(n) and a width of n - i is correct but reads backwards and is where off-by-one bugs hide. Reaching for the direct form signals comfort with range semantics.

> **Common Pitfall**
>
> Adding leading spaces for center alignment when the problem asks only for left-aligned asterisks, or looping to 0 and emitting an empty final row.

---

## Common follow-up questions

- How would you center-align the triangle? _(Tests prepending leading spaces: "' ' * (n - w)" before the asterisks.)_
- How would you render a full diamond? _(Tests composing an upward and an inverted triangle.)_
- What if the caller wants a single multi-line string instead of a list? _(Tests joining the list with the newline separator into one string.)_

## Related

- [All practice problems](https://datadriven.io/problems)
- [Mock interview mode](https://datadriven.io/interview/top_heavy)
- [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). DataDriven is the data engineering interview community. Live code execution in SQL, Python, and Spark sandboxes. Every feature is open to every member.