# Exercise: The Company Org Chart

The staff app's org chart page needs a head count for any manager, the people at the bottom of the chart, and the whole chart printed with each team indented, each from one visit per person: build all three, and the tests check them.

Journeyman · Data Structures · Updated November 17, 2026

By Gregor Dzierzon

Here's the ticket from the bike company's HR team:

> The org chart page in the staff app needs three things from the chart. Today we're 11 people; next year we'll be thousands, so each one should look at each person once, not reread the whole company for every name.
>
> 1. **Head count.** For any person, how many people work under them, all the way down. Ava's should say 10.
> 2. **The bottom of the chart.** Everyone at the deepest level, left to right. Today that's our two newest apprentices.
> 3. **The chart, printed.** One line per person, each team indented under its manager, two spaces for every step down from Ava.

Since the last lesson the company has grown: Farah now trains two apprentices, Hana and Jo, and Ivy has hired Kim. Each of the three jobs visits all 11 people, and the order you visit them in is the whole exercise.

## Before you start

You need one language set up, as in *Setup: VS Code and Your Language*. The exercise is written in all five of the course's languages; pick the one you use.

## Get the exercise code

Open this lesson's GitHub repository, [github.com/ask-codey/exercise-ds-org-chart](https://github.com/ask-codey/exercise-ds-org-chart), also linked from the **Exercise** box beside the lesson. Clone it into your course folder. In a terminal, in that folder:

```bash
git clone https://github.com/ask-codey/exercise-ds-org-chart.git
```

Git makes a folder, `exercise-ds-org-chart`, with the exercise code inside.

The repository has a folder for each language: `csharp`, `java`, `python`, `javascript` and `typescript`. Each folder holds:

- **your code:** one file with three functions to finish (the repository's `README.md` names the file for each language);
- **the `Employee` class:** a person on the chart, with a name and a list of reports, the same node as in *Print the Chart, Count the Teams: Walking a Tree in the Right Order*. You don't need to change it;
- **the tests** that check your functions. You don't need to change them either;
- **a demo program** that prints the bike company's chart with your functions.

In VS Code, choose **File > Open Folder** and open **your language's folder**, not the whole repository.

## What to do

Every function is already there, with its parameter, and returns `0` or an empty list for now. The comment above each one says what it should return, with examples, and ends with a **Growth rate:** line. Here is the first one in Python, as the starter has it (the other languages say the same in their own syntax):

```python
def count_under(person):
    """
    Count everyone who works under a person: their reports, their
    reports' reports, and so on all the way down. Don't count the
    person themselves.

    count_under(ava) -> 10
    count_under(chloe) -> 4   (Farah, Hana, Jo and Gus)
    count_under(dev) -> 0     (a leaf: nobody reports to Dev)

    Growth rate: (write it here)
    """
    return 0
```

1. **The head count.** Finish `countUnder` (`count_under` in Python, `CountUnder` in C#). Ava has 10 people under her; Dev, a leaf, has none.
2. **The bottom of the chart.** Finish `deepestTeam`. It returns the names of everyone at the deepest level of the chart, left to right: for the bike company, Hana and Jo.
3. **Print the chart indented.** Finish `chartLines`. It returns one line per person, each manager before their team, with two spaces in front of a name for every step down from the root.
4. **Name the growth rates.** On each function's **Growth rate:** line, write its Big-O and a few words on why.

## Check your work

Each language has its own command, run in the terminal in your language's folder (**Terminal > New Terminal** in VS Code). The repository's `README.md` has the full list, including the one-time setup for Python (a virtual environment and pytest) and TypeScript (`npm install`).

| Language | Run the tests | Run the demo |
|---|---|---|
| C# | `dotnet test` | `dotnet run --project OrgChart` |
| Java | `./mvnw test` (`.\mvnw test` in PowerShell) | `java src/main/java/Demo.java` |
| Python | `python -m pytest` | `python demo.py` |
| JavaScript | `npm test` | `node demo.js` |
| TypeScript | `npm test` | `node demo.ts` |

At first all 8 tests fail. That's expected: each function you finish turns its tests green. A failing test's message starts with **Not yet:** and says what it expected, for example *Not yet: Farah, Hana, Jo and Gus work under Chloe: 4 people.*

&lt;details>
&lt;summary>Stuck?&lt;/summary>

- **Each job needs its own order.** All three visit every person once; what changes is *when* a person is handled compared with their team. The table *Which walk for which job* in *Print the Chart, Count the Teams: Walking a Tree in the Right Order* has all three.
- **The head count needs the parts first.** Everyone under a manager is each of their reports, plus everyone under that report: add 1 for the report, then ask the same question of the report. Counting the team before adding it in is a **post-order** walk.
- **The bottom of the chart is about levels.** Keep one level in a list, and build the next level from all of that level's reports. When the next level would be empty, the one you have is the deepest. That's a **level-order** walk.
- **The printed chart needs each manager above their team.** Write a helper that takes a person and how deep they are. It adds the person's line, then calls itself for each report with the depth plus one: a **pre-order** walk.
- **Stuck on a growth rate?** Ask how many times each person is visited. A walk that visits everyone once grows the way reading a list once does.

&lt;/details>

## When you're done

Every test is green, and the demo prints the same lines in every language:

```text
The chart:
Ava
  Ben
    Dev
    Emi
  Chloe
    Farah
      Hana
      Jo
    Gus
  Ivy
    Kim
Everyone under Ava: 10
Everyone under Chloe: 4
Deepest team: Hana, Jo
```

Now look back at your three functions. Each one visits every person once, but in a different order. Which of them could you rewrite with a different traversal and still get the right answer, and which one only works in its own order?

&lt;details>
&lt;summary>Why Should I Choose This?&lt;/summary>

- **Rows of (name, manager), reread for each person:** a head count for Ava would read every row once for each person it finds: O(n²), 100 million checks at 10,000 staff.
- **Rescanning the chart once per level:** finds the bottom of the chart, but walks the whole company once for every level, O(n·h).
- **Printing a person, then their team, for the head count too:** the printed chart comes out right, but a manager's count is needed before their team has been counted, so it comes too late.
- **A tree, walked in the order the job needs:** post-order for the counts (parts before totals), level-order for the bottom (one level at a time, with a queue), pre-order for the printed chart (each manager on top of their team). Each visits every person once: O(n), however big the company gets.

&lt;/details>

## Where else does this shape show up?

You've worked with "everything under one thing, each with exactly one parent". Before you open the answers, think of three other places, on a screen or in daily life, where things sit inside or under other things this way, and which walk each one's most common job would need.

&lt;details>
&lt;summary>Some answers&lt;/summary>

- **A web page's layout** is boxes inside boxes: the browser draws each box before the boxes inside it (pre-order).
- **Comment threads** are replies to replies: showing a thread indented is the printed chart again.
- **Folders on your computer:** a folder's size is the total of what's inside it (post-order), and deleting a folder empties it first (post-order too).
- **A tournament bracket:** each match's winner comes from the two matches under it, so the final is decided last (post-order).
- **A menu with submenus**, or a book's table of contents: each heading above its parts (pre-order).
- **Not a tree:** a real family tree, where everyone has two parents, or a subway map, where lines loop back. Those are Module 7's graphs.

&lt;/details>

## Submit your exercise

The exercise is how you finish this lesson. When your work is done, put it on GitHub in a repo of your own, then submit that repo's address here.

1. On GitHub, click **+**, then **New repository**. Name it (for example `exercise-ds-org-chart`), choose **Public**, and click **Create repository**. Leave out the README: your folder already has one.
2. In a terminal in your exercise folder (Git Bash on Windows), send your work to your new repo. Point your copy at your own repo, then commit and push:

   ```bash
   git remote set-url origin https://github.com/your-name/exercise-ds-org-chart.git
   git add .
   git commit -m "My solution"
   git push -u origin main
   ```

3. Paste your repo's address, such as `https://github.com/your-name/exercise-ds-org-chart`, into **Submit your exercise** on this page. The site checks that the repo is public and holds your commits, and then the lesson is complete.

Changed something after you submitted? Push it, and submit again: your latest submission is the one that counts.

## So what

Counting a team, finding the bottom of a chart and printing it are three everyday jobs on one tree, and you picked the right walk for each. The same walks run file browsers, comment threads and every web page's layout. But the chart can't help you *find* someone: nothing says which branch they're in. The next chapter puts a tree's children in order, so a search can choose one branch and skip the rest.

## Key terms

- [Tree](https://www.ask-codey.com/glossary/tree)
- [Tree Height](https://www.ask-codey.com/glossary/tree-height)
- [Pre-Order Traversal](https://www.ask-codey.com/glossary/pre-order-traversal)
- [Post-Order Traversal](https://www.ask-codey.com/glossary/post-order-traversal)
- [Level-Order Traversal](https://www.ask-codey.com/glossary/level-order-traversal)

## Related

- [Who Works Under Whom? Trees](https://www.ask-codey.com/lessons/ds-trees)
- [Print the Chart, Count the Teams: Walking a Tree in the Right Order](https://www.ask-codey.com/lessons/ds-tree-traversals)

Source: https://www.ask-codey.com/lessons/exercise-ds-org-chart
