They asked you to write by hand what a library already does

The task is small, and you know a library that does it in one call. The interviewer says no libraries, no search, no assistant. The first thirty seconds decide how the rest of the round goes: whether you spend them arguing, typing in silence, or saying what the hand-written version has to handle.

The situation

An original task, said aloud: "Durations arrive as strings like 90m, 2h or 45s. Write a function that says which of two durations is longer."

At work you would not write this. In Java, Duration.parse already reads the ISO form, and Duration.parse("PT1H30M").equals(Duration.parse("PT90M")) is true. The interviewer knows that too. They are not asking whether you can find a library. They want to watch you decide what the input is allowed to look like, and that decision is exactly what a library hides from you.

What goes wrong

The quickest version treats every duration as one number followed by one letter:

UNITS = {"h": 3600, "m": 60, "s": 1}

def naive_seconds(text):
    return int(text[:-1]) * UNITS[text[-1]]

On 90m, 2h and 45s it returns 5400, 7200 and 45. It passes every example in the task. Then the interviewer tries 1h30m, and int("1h30") raises ValueError. They try 90, with no unit, and the error is KeyError: '0', because the last digit was looked up as a unit. Neither failure is a typo. Both come from one shape you assumed and never said.

A version that reads number-unit pairs in a loop handles the compound form and refuses the rest:

def seconds(text):
    total, number = 0, ""
    for ch in text:
        if ch in "0123456789":
            number += ch
        elif ch in UNITS and number:
            total += int(number) * UNITS[ch]
            number = ""
        else:
            raise ValueError(f"bad duration: {text!r}")
    if number or not text:
        raise ValueError(f"bad duration: {text!r}")
    return total

def compare(a, b):
    x, y = seconds(a), seconds(b)
    return (x > y) - (x < y)

seconds("1h30m") is 5400, so compare("90m", "1h30m") is 0 and compare("2h", "90m") is 1. An empty string, a bare 90, a lone m and h30m all raise. But look at what it still accepts without anyone deciding: 1h1h gives 7200, and 30m1h gives 5400. Those may be fine. They may be a bug in the caller that you are now hiding. Only the person who wrote the task knows, and they will tell you if you ask.

What to say out loud

Four sentences, before the first line.

Name the tool once, then let it go: "At work I'd reach for a parser that already exists, mostly for its error handling. Here I'll write the small one, and I'll tell you which cases it covers." One sentence, no argument. If you think the round measures the wrong thing, that is a fair view, and the place for it is your feedback afterwards, not the first minute.

Turn the input into shapes, each with an example: "I see three shapes: a single pair like ninety m, a compound like one h thirty m, and things I'd reject, like a bare ninety or an empty string. Is compound in scope?" A shape with an example is a question the interviewer can answer in one word.

Say the decision nobody asked for: "Repeated or out-of-order units, like one h one h, I'll accept and add up unless you'd rather I reject them." Saying it costs five seconds. Discovering it in their test costs the rest of the round.

Say the contract of the answer: "Compare returns minus one, zero or one, so ninety m and one h thirty m are equal." Now the interviewer can check your result against something you both agreed.

If you need a moment, mark it: "Give me twenty seconds to list the shapes before I type." Then list them where they can see.

The exercise

  1. Pick three small tasks you would normally solve with one library call: a duration, a version string such as 1.10.2 against 1.9, a date range.
  2. For each one, write every input shape you can think of, with one example each, and mark each shape accept or reject.
  3. Write the one-sentence opener that names the library and moves on. Say it aloud with a timer; over ten seconds is too long.
  4. Solve one task by hand in Java or Python, then run the strangest example from your list through it in your head before running it for real.
  5. Write down one input your code accepts that you never decided to accept. That is the sentence to add to your opener next time.

Where this fits in Gadfly

Gadfly has four rooms, and in the algorithms room you code in Java or Python while the tutor speaks English. In tutor mode it asks the next question and never reveals the solution, so "what should happen on a bare number?" is the kind of question it puts to you. Mock mode behaves like an interviewer instead: no coaching during the round, and a debrief at the end quoted from the record of what you said. It does not run an AI-assisted round.

Published 10 November 2026 · Explain it out loud · all notes · feed

This note was drafted by Gadfly's scout — an automated writing agent run by Dmytro Hryshchenko — and checked by rules against the product's claims registry, not read by a person before it went live. The exercise is original. Spotted a mistake? [email protected].