# Exercise: Race Two Solutions

Product needs to know, with numbers, how the repeats report grows and how long it will take for 64,000 songs: build a stopwatch in your language, race two finished solutions at four sizes, keep the fastest of five runs, name each growth rate from your measurements, and predict a time you haven't measured.

Apprentice · Software Development · Updated November 17, 2026

By Gregor Dzierzon

Here's the ticket from the music app's product manager:

> **From:** the product manager
>
> Before we launch the "repeats report" for big libraries, I need two answers with numbers, not guesses: how does the report's time grow as a library grows, and roughly how long will it take for a 64,000-song library? The plays counter needs the same check. We don't have a 64,000-song test library yet.
>
> 1. **Time both features fairly** on your computer, at 500 to 4,000 songs, so one busy moment can't spoil the numbers.
> 2. **Name each one's growth rate** from your measurements.
> 3. **Predict the time** at a size you haven't run.

Two solutions are finished and ready to race: **count plays**, which reads the playlist once to count how often a song appears, and **count repeats**, the report, which compares every song with every other song to find pairs that share a title. Your job is the stopwatch and the judging: time both at four sizes, keep the fastest of five runs, read each one's growth rate from your own numbers, and then predict how long the report would take on a playlist you never ran. It's *Is It Really Slow? Measure Your Own Code* with you holding the stopwatch.

## Before you start

You need one language set up, as in *Setup: VS Code and Your Language*, and to know how to run the tests, as in *Running the Exercise Tests*. 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-big-o-race-two-solutions](https://github.com/ask-codey/exercise-big-o-race-two-solutions), 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-big-o-race-two-solutions.git
```

Git makes a folder, `exercise-big-o-race-two-solutions`, with the exercise code inside.

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

- **the two solutions to race**, finished (`Given` in C# and Java, `given` in the others). You don't change them;
- **your code:** five functions to finish (the repository's `README.md` names the file for each language);
- **the tests** that check your functions. You don't need to change them;
- **a demo program** that races the two solutions at 500, 1,000, 2,000 and 4,000 songs and prints a table of times.

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 and returns `0` or empty text for now. The comment with each one says what it should return, with examples. Here is the first one in Python, as the starter has it (the other languages say the same with their own stopwatch):

```python
def time_task(task):
    """
    Run the task once and return how many milliseconds it took, using
    time.perf_counter() before and after (seconds: multiply by 1000).

    time_task(lambda: count_repeats(playlist)) -> about 150 (it depends on your computer)
    """
    return 0
```

1. **Build the stopwatch.** Finish `timeTask` (`time_task` in Python, `TimeTask` in C#): read the clock, run the task, read the clock again, and return the difference in milliseconds.
2. **Keep the fastest.** Finish `fastest`: the smallest time in a list. The demo times each solution five times at each size and keeps the fastest, as *Is It Really Slow? Measure Your Own Code* explains.
3. **Run the demo,** and look at the table it prints. Divide each row's time by the row above.
4. **Name the growth rates.** Finish `countPlaysRate` and `countRepeatsRate`: return `"O(1)"`, `"O(n)"` or `"O(n²)"` (`"O(n^2)"` is fine too) for each solution, from what your measurements show.
5. **Predict.** Finish `predictTime`: from a time now, a growth rate and how many times bigger the data gets, work out the new time. O(1) stays the same, O(n) grows by the factor, and O(n²) by the factor times itself.

## 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 RaceTwoSolutions` |
| 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: O(n²) work at 10 ms takes about 40 ms with twice the data.* One test times a task that's built to take 20 milliseconds, and checks that your stopwatch says about 20.

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

- **Clock units differ.** `Stopwatch` (C#) gives milliseconds directly; Java's `System.nanoTime()` counts nanoseconds (divide by 1,000,000); Python's `time.perf_counter()` counts seconds (multiply by 1,000); `performance.now()` (JavaScript, TypeScript) is already in milliseconds.
- **Start `fastest` with the first time,** then walk the list, keeping whichever is smaller.
- **The count plays times are tiny**, thousandths of a millisecond, so they're the noisiest. Look at the overall trend from 500 to 4,000 songs, not one row.
- **`predictTime` is the doubling table in reverse**: O(n) times 2 for twice the data, O(n²) times 4.

&lt;/details>

## When you're done

Every test is green, and the demo prints a table like this one, from one run of the Python version. Your times will be different, and different again each time you run it:

```text
Songs   count plays (ms)   count repeats (ms)
500   0.005   2.547
1000   0.011   11.006
2000   0.022   43.923
4000   0.041   177.387
count plays grows like O(n); count repeats grows like O(n²)
Predicted count repeats time for 8000 songs: 709.548 ms
```

Now check your prediction. Add 8,000 to the demo's list of sizes and run it again: how close did `predictTime` get? Then answer the ticket: a playlist of 64,000 songs is seven doublings past 500. Without running anything, roughly how many times slower than the 500-song time would the repeats report be, and what would you tell product?

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

Open this once your tests are green.

- **One run, read off the stopwatch:** a computer is always busy with something else, and the first run is often slower while the language gets ready. One run can be twice too slow, and product would plan around a number that was never true.
- **Counting steps without measuring:** counting predicts the growth rate, and it's right, but "is it really slow?" is a question about a real machine. The measurements show the same O(n) and O(n²) the step counts did.
- **Comparing your times with someone else's:** different computers give different numbers. The ratio when the data doubles, about 2 for O(n) and about 4 for O(n²), is the same on every computer, so that's what you report.
- **The fastest of five runs, at doubling sizes:** fair numbers, a growth rate read from the ratios, and a prediction anyone can check. What it can't promise: a prediction 16 times past your largest run assumes the growth rate holds that far. When the 64,000-song library exists, measure it.

&lt;/details>

## Where else does this shape show up?

This chapter's other big idea was in *Answer It in One Step: Trading Memory for Time*: spend some memory once, so a question asked again and again takes one step. Before you open the answers, think of three other places, in code or in daily life, where something is worked out or written down ahead of time so it never has to be worked out again.

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

- **A hash table** (Module 5) spends memory on its buckets so finding something by name takes one step.
- **Memoization** (Module 3) writes down each answer a function works out, so it's never worked out twice.
- **A database index** keeps a sorted list of one column, so a search skips the rest of the table.
- **A spreadsheet's running total column** answers "sales so far this month" for any day at a glance.
- **A phone keeps small copies of your photos** to scroll through your library quickly, instead of loading every full-size photo.

&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-big-o-race-two-solutions`), 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-big-o-race-two-solutions.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-big-o-race-two-solutions`, 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.

## What Module 2 unlocks

You can now count the cost of code, measure it, and say how it grows. Module 2, *Things in a Line*, puts that to work on the first problem every app has: keeping a list at all. Why can "play track 214" be one step, and what happens when the playlist outgrows the room it was given? Then "play next" without moving every song, undo that only ever needs the newest change, and a line that serves people in the order they came. Each one starts where the last one breaks.

## So what

You raced two solutions the way developers do: with a stopwatch, five runs, and the ratios between sizes. And you turned a growth rate into a prediction you could check. That's the skill behind every "will it still be fast with ten times the users?" conversation.

## Key terms

- [Benchmark](https://www.ask-codey.com/glossary/benchmark-software-development)
- [Growth Rate](https://www.ask-codey.com/glossary/growth-rate)
- [Time Complexity](https://www.ask-codey.com/glossary/time-complexity)

## Related

- [Is It Really Slow? Measure Your Own Code](https://www.ask-codey.com/lessons/big-o-measure-your-code)
- [Will It Survive a Million Users? The Doubling Table](https://www.ask-codey.com/lessons/big-o-doubling-table)
- [Why the Duplicates Button Crawls: O(n²)](https://www.ask-codey.com/lessons/big-o-quadratic)

Source: https://www.ask-codey.com/lessons/exercise-big-o-race-two-solutions
