# Your Turn at the Whiteboard: Solving a Problem Out Loud

Work an interview problem yourself, out loud, on a road-trip playlist: decide what to ask, plan and price a first idea, improve it with a structure, code it and test it, deciding at each step before you look, then name the four steps you used: clarify, plan, code, test.

Journeyman · Data Structures · Updated November 17, 2026

By Gregor Dzierzon

## After this lesson you can

- Work a problem through out loud in four steps: clarify, plan, code, test
- Write test cases for a problem, including edge cases, before calling it done

## Key takeaways

- Solving out loud has four steps: clarify the problem, plan an approach and its Big-O, code it while explaining, and test it.
- Clarifying questions come first, about the input, the output and the odd cases, because they often change the answer.
- It's fine to start with the slow, obvious plan; say its Big-O, then improve it with the right structure.
- Test with the normal case and the edge cases (empty, one item, no answer, repeats) before you say you're done.

**Before this:** *The Feature Is Slow: Which Structure Fixes It?* worked a ticket with the lesson leading. In an interview, nobody leads: you're handed a problem and a whiteboard, and someone listens to how you think.

So this lesson hands the problem to you. At each step it stops and asks what you'd do; decide, say it out loud, then open the fold to compare.

> **The problem:** a road trip has a set number of minutes left. Find two songs on the playlist whose lengths add up to exactly that many minutes.

The interviewer stops there and waits. Whatever you say next, they're listening to how you think, not only for an answer.

## Your first move

Before any code, what would you ask? Take a minute and write down three or four questions. Think about the input, the output, and the odd cases.

&lt;details>
&lt;summary>The questions worth asking, and the interviewer's answers&lt;/summary>

- "Are the lengths whole minutes?" (Yes.)
- "Can the same song be used twice?" (No: two different songs.)
- "Can two different songs have the same length?" (Yes.)
- "What if no two songs fit?" (Say there's no pair.)
- "If several pairs fit, which one?" (Any one. Return the first pair you find.)
- "How long can the playlist be?" (Up to 100,000 songs.)

The last answer matters most: 100,000 songs rules out anything that's O(n²). If you didn't ask it, that's the one to remember. A question about size is a question about which Big-O will do.

&lt;/details>

## Your plan

What's the first plan that would work, however slow? Say it, and say its Big-O for 100,000 songs.

&lt;details>
&lt;summary>The obvious plan&lt;/summary>

"Compare every song with every other song, like the handshakes in *Why the Duplicates Button Crawls: O(n²)*. That's O(n²): about 5 billion pairs for 100,000 songs. Too slow."

Saying the slow plan out loud isn't a mistake. It shows you can solve it, and it gives you something to improve. Textbooks call it **brute force**.

&lt;/details>

Now improve it. For one song, you know exactly what length its partner must have. What question are you asking, again and again, and which structure from the course answers it in one step?

&lt;details>
&lt;summary>The better plan&lt;/summary>

"For each song, the length I need is the minutes left minus this song's length. The question is 'have I already passed a song that long?', and a dictionary of the lengths I've passed answers it in O(1) on average. So one pass through the playlist is O(n) time, for O(n) memory."

That's the trade from *The Feature Is Slow: Which Structure Fixes It?*: a set or a dictionary makes "have I seen this?" cheap.

&lt;/details>

## Your code

Write it yourself before you read ours, saying what each part is for as you go. Then compare. In ours, the dictionary maps a length to the first song with that length, and a song is checked **before** it's added, so it can't pair with itself.

```csharp
List<string> TwoSongs(List<(string Title, int Minutes)> songs, int minutesLeft)
{
    Dictionary<int, string> seen = new Dictionary<int, string>();
    foreach ((string title, int minutes) in songs)
    {
        int needed = minutesLeft - minutes;
        if (seen.ContainsKey(needed))
        {
            return new List<string> { seen[needed], title };
        }
        if (!seen.ContainsKey(minutes))
        {
            seen[minutes] = title;
        }
    }
    return new List<string>();
}

void Show(string label, List<string> pair)
{
    Console.WriteLine(label + ": " + (pair.Count == 0 ? "no pair" : string.Join(" + ", pair)));
}

List<(string Title, int Minutes)> playlist = new List<(string Title, int Minutes)> { ("Midnight Bus", 4), ("Lemon Skies", 3), ("Slow Rain", 6), ("Paper Boats", 4), ("Neon Garden", 5) };
Show("9 minutes", TwoSongs(playlist, 9));
Show("8 minutes", TwoSongs(playlist, 8));
Show("20 minutes", TwoSongs(playlist, 20));
Show("Empty playlist", TwoSongs(new List<(string Title, int Minutes)>(), 5));
Show("One 5-minute song, 10 minutes", TwoSongs(new List<(string Title, int Minutes)> { ("Solo", 5) }, 10));
```

```text
9 minutes: Lemon Skies + Slow Rain
8 minutes: Midnight Bus + Paper Boats
20 minutes: no pair
Empty playlist: no pair
One 5-minute song, 10 minutes: no pair
```

```java
record Song(String title, int minutes) {}

List<String> twoSongs(List<Song> songs, int minutesLeft) {
    Map<Integer, String> seen = new HashMap<>();
    for (Song song : songs) {
        int needed = minutesLeft - song.minutes();
        if (seen.containsKey(needed)) {
            return List.of(seen.get(needed), song.title());
        }
        seen.putIfAbsent(song.minutes(), song.title());
    }
    return List.of();
}

void show(String label, List<String> pair) {
    IO.println(label + ": " + (pair.isEmpty() ? "no pair" : String.join(" + ", pair)));
}

void main() {
    List<Song> playlist = List.of(new Song("Midnight Bus", 4), new Song("Lemon Skies", 3), new Song("Slow Rain", 6), new Song("Paper Boats", 4), new Song("Neon Garden", 5));
    show("9 minutes", twoSongs(playlist, 9));
    show("8 minutes", twoSongs(playlist, 8));
    show("20 minutes", twoSongs(playlist, 20));
    show("Empty playlist", twoSongs(List.of(), 5));
    show("One 5-minute song, 10 minutes", twoSongs(List.of(new Song("Solo", 5)), 10));
}
```

```text
9 minutes: Lemon Skies + Slow Rain
8 minutes: Midnight Bus + Paper Boats
20 minutes: no pair
Empty playlist: no pair
One 5-minute song, 10 minutes: no pair
```

```python
def two_songs(songs, minutes_left):
    seen = {}
    for title, minutes in songs:
        needed = minutes_left - minutes
        if needed in seen:
            return [seen[needed], title]
        if minutes not in seen:
            seen[minutes] = title
    return []


def show(label, pair):
    print(f"{label}: " + (" + ".join(pair) if pair else "no pair"))


playlist = [("Midnight Bus", 4), ("Lemon Skies", 3), ("Slow Rain", 6), ("Paper Boats", 4), ("Neon Garden", 5)]
show("9 minutes", two_songs(playlist, 9))
show("8 minutes", two_songs(playlist, 8))
show("20 minutes", two_songs(playlist, 20))
show("Empty playlist", two_songs([], 5))
show("One 5-minute song, 10 minutes", two_songs([("Solo", 5)], 10))
```

```text
9 minutes: Lemon Skies + Slow Rain
8 minutes: Midnight Bus + Paper Boats
20 minutes: no pair
Empty playlist: no pair
One 5-minute song, 10 minutes: no pair
```

```javascript
function twoSongs(songs, minutesLeft) {
  const seen = new Map();
  for (const [title, minutes] of songs) {
    const needed = minutesLeft - minutes;
    if (seen.has(needed)) {
      return [seen.get(needed), title];
    }
    if (!seen.has(minutes)) {
      seen.set(minutes, title);
    }
  }
  return [];
}

function show(label, pair) {
  console.log(`${label}: ${pair.length === 0 ? "no pair" : pair.join(" + ")}`);
}

const playlist = [["Midnight Bus", 4], ["Lemon Skies", 3], ["Slow Rain", 6], ["Paper Boats", 4], ["Neon Garden", 5]];
show("9 minutes", twoSongs(playlist, 9));
show("8 minutes", twoSongs(playlist, 8));
show("20 minutes", twoSongs(playlist, 20));
show("Empty playlist", twoSongs([], 5));
show("One 5-minute song, 10 minutes", twoSongs([["Solo", 5]], 10));
```

```text
9 minutes: Lemon Skies + Slow Rain
8 minutes: Midnight Bus + Paper Boats
20 minutes: no pair
Empty playlist: no pair
One 5-minute song, 10 minutes: no pair
```

```typescript
function twoSongs(songs: [string, number][], minutesLeft: number): string[] {
  const seen = new Map<number, string>();
  for (const [title, minutes] of songs) {
    const needed = minutesLeft - minutes;
    const partner = seen.get(needed);
    if (partner !== undefined) {
      return [partner, title];
    }
    if (!seen.has(minutes)) {
      seen.set(minutes, title);
    }
  }
  return [];
}

function show(label: string, pair: string[]): void {
  console.log(`${label}: ${pair.length === 0 ? "no pair" : pair.join(" + ")}`);
}

const playlist: [string, number][] = [["Midnight Bus", 4], ["Lemon Skies", 3], ["Slow Rain", 6], ["Paper Boats", 4], ["Neon Garden", 5]];
show("9 minutes", twoSongs(playlist, 9));
show("8 minutes", twoSongs(playlist, 8));
show("20 minutes", twoSongs(playlist, 20));
show("Empty playlist", twoSongs([], 5));
show("One 5-minute song, 10 minutes", twoSongs([["Solo", 5]], 10));
```

```text
9 minutes: Lemon Skies + Slow Rain
8 minutes: Midnight Bus + Paper Boats
20 minutes: no pair
Empty playlist: no pair
One 5-minute song, 10 minutes: no pair
```

## Your tests

Before you say "done": which inputs would you try, and what should each one give? Aim for five, each checking something different.

&lt;details>
&lt;summary>Our five test cases&lt;/summary>

The five lines the program prints are the test plan, written down before calling it done. Each **test case** is an input and the answer you expect:

| Test case | What it checks |
|---|---|
| 9 minutes | The normal case: Lemon Skies (3) and Slow Rain (6) |
| 8 minutes | Two different songs with the same length (4 + 4) still pair up |
| 20 minutes | No pair: the code says so instead of crashing or making one up |
| Empty playlist | The smallest input of all |
| One 5-minute song, 10 minutes | A song can't pair with itself, because it's checked before it's added |

The last four are **edge cases**: the unusual inputs at the edges of what's allowed, where bugs hide.

&lt;/details>

Say each case out loud as you trace it: "Midnight Bus, 4 minutes: I need 4, nothing seen yet, so remember 4. Lemon Skies, 3: I need 5, not seen..." A bug usually shows up halfway through a sentence like that.

**Try it.** Swap the two `if` blocks in the loop, so a song is added before it's checked, and run the last case again. "Solo" now pairs with itself, and the edge case catches the bug.

## What you did has a name

You asked, then planned, then wrote, then checked. Those four moves are what interviewers mean by **solving a problem out loud**, and they work the same at work, when you explain a fix to a teammate, and on your own when you're stuck:

1. **Clarify.** Repeat the problem in your own words and ask about the input, the output and the odd cases.
2. **Plan.** Say an approach and its Big-O before you write code. Starting with brute force is fine.
3. **Code.** Write it, saying what each part is for.
4. **Test.** Run through examples by hand, including the edge cases.

![Four panels in a row, labeled Clarify, Plan, Code and Test: a person at a whiteboard asking a question, the same person sketching two boxes joined by an arrow, the same person typing at a laptop, and the same person checking a short list of examples with ticks beside them](https://fsdtrainingmedia.blob.core.windows.net/public/images/2026/11/16ad71ab36754f169e1946c4a3dbc803.webp)

## Check yourself

2 questions to try on the lesson page.

## So what

Clarify, plan, code, test is how you turn a blank whiteboard into a working answer: questions first, Big-O before code, and edge cases before "done". You've now done it once on your own. The next lesson gives you seven more problems to do it with.

## Key terms

- [Edge Case](https://www.ask-codey.com/glossary/edge-case)
- [Test Case](https://www.ask-codey.com/glossary/test-case)
- [Brute Force](https://www.ask-codey.com/glossary/brute-force)
- [Big-O Notation](https://www.ask-codey.com/glossary/big-o-notation)

## Cheat sheets

- [Your Turn at the Whiteboard: Solving a Problem Out Loud](https://www.ask-codey.com/cheat-sheets/ds-solving-out-loud-cheat-sheet)

## Related

- [The Feature Is Slow: Which Structure Fixes It?](https://www.ask-codey.com/lessons/ds-choosing-a-structure)
- [Have I Seen This Before? Sets](https://www.ask-codey.com/lessons/ds-sets)
- [Why the Duplicates Button Crawls: O(n²)](https://www.ask-codey.com/lessons/big-o-quadratic)

Source: https://www.ask-codey.com/lessons/ds-solving-out-loud
