Skip to content

Contact

Mexico City, Mexico
Available for remote and on-site projects.

Professional networks

LinkedIn · GitHub · Email
Applied engineering, measurable results.

First steps before writing a line of code

· 7 min read · Samuel Torres

By Samuel Torres — Software & Cloud Engineer. 23+ production sites.

On November 21, 2026 I took part in the Google Core Hackathon End of Year on HackerRank: 4 algorithm problems and 150 minutes to solve them. The official results aren't out yet (they take at least a week), so this post isn't about how I did. It's about something I learned the hard way and now do on every problem: I don't write code until I've finished a short routine.

The idea is simple: the first five minutes of each problem decide how much you'll suffer in the next forty. If you start typing with the first idea that pops into your head, odds are that halfway through you'll find out the statement asked for something else, your solution doesn't finish in time, or you're printing the answer in the wrong format. With the clock running, any of those costs you a lot more than five minutes.

First the six steps, then I'll walk through all of them on one of the problems from the challenge, Closest Numbers.

The 6-step routine

1. Read the statement twice

The first read is to get what the problem is about. The second is to catch what you missed: a word like "all", "distinct" or "in ascending order" changes the whole problem. Statements tend to be long and wrapped in a story. The second read is where you strip the story away and keep only what you're actually being asked.

2. Read the constraints

This is the part almost everyone skips, and it's the one that tells you the most. The constraints say how many items you'll get at most and how big they can be. With that, before writing anything, you know whether a brute-force solution will make it or time out. Sorting 10 numbers is not the same as sorting 100,000.

3. Work the example by hand

Take the example from the statement and solve it yourself, on paper or in a comment, no code. If you can't solve it by hand, you don't understand the problem yet, and no amount of code will fix that. If you can, you've just found the steps your program needs to follow.

4. Check the template

HackerRank gives you a template with a function already declared: a name, some parameters and what it's expected to return. Read it before you write. Check what the function receives, whether you have to return a value or print it, and in what type. The template depends on the language you pick. In the challenge I started in C and switched to PHP, which is what I use every day for WordPress and plugins; switch languages and the template changes, so you read it again.

5. Write the idea in words

Before the code, one sentence. If the idea doesn't fit in a sentence or two, it's still tangled in your head. That sentence is your map: when you get lost among syntax errors, you go back to it and remember what you were trying to do.

6. Check the output format

Your output isn't read by a person: a program compares it against the expected answer, character by character. One extra space at the end, a missing line break or the numbers in a different order can mark an answer as wrong even when the logic was right. So before you submit, compare your output with the example's, character by character.

The routine in practice: Closest Numbers

Closest Numbers was one of the four problems in the challenge, and one of the ones I solved. In short: you get a list of numbers and have to find the pairs that are closest to each other, the ones with the smallest difference. If several pairs share that difference, you report all of them.

Step by step.

Steps 1 and 2: statement and constraints

After two reads, the problem has two parts: find the minimum difference, then find every pair that has it. It's not "the closest pair", singular; it's every pair that ties.

Then the constraints: the list can hold up to 100,000 numbers. That one fact drives everything else.

The first idea anyone has is to compare every number against every other one. With 100,000 numbers that's 100,000 × 99,999 / 2 pairs, roughly 5 billion comparisons. Same story if you write your own bubble sort: in the worst case it does on the order of n²/2 comparisons, and with n = 100,000 that's 100,000² / 2 = 5,000,000,000. It won't finish in time. That's a guaranteed timeout.

PHP's sort(), on the other hand, already ships an efficient sort, which as a general rule works on the order of n·log n. With n = 100,000, log₂ of 100,000 is almost 17, so we're talking about 1.7 million comparisons or so. The gap between those two numbers is the gap between passing and failing the big test cases.

Step 3: the example by hand

Closest Numbers by hand: sort and compare only neighbors
Closest Numbers by hand: sort and compare only neighbors

I take a small list: [6, 2, 4, 10].

Left as is, I'd have to compare 6 with 2, with 4 and with 10; then 2 with 4 and with 10; and so on. But if I sort it first, it becomes [2, 4, 6, 10], and something shows up that's easy to see by hand:

NeighborsDifference
2 and 42
4 and 62
6 and 104

The minimum difference is 2, and two pairs have it: (2, 4) and (4, 6).

What matters about doing it by hand is what you discover: in a sorted list, the number closest to any other is always right next to it. There's no need to compare 2 with 10; if 2 and 4 are already 2 apart, 10 can't be any closer to 2. That's why you only compare neighbors, and with n numbers there are n − 1 neighbor pairs, not 5 billion pairs.

Step 4: the template

I can't tell you the exact function name or return type from this challenge, and I'm not going to make it up. What I can tell you is what to look for: which parameter holds the list, whether the function has to return an array or print text, and whether the template already includes the code that reads the input. If it does, leave it alone: your job is inside the function.

Step 5: the idea in one sentence

Sort, then compare neighbors.

A bit longer: sort the list, walk the neighbors once to find the minimum difference, then walk them again to collect the pairs with that difference.

With that sentence, the code almost writes itself:

<?php
function closestNumbers($arr) {
    sort($arr);
    $n = count($arr);

    // First pass: the minimum difference between neighbors
    $min = PHP_INT_MAX;
    for ($i = 1; $i < $n; $i++) {
        $diff = $arr[$i] - $arr[$i - 1];
        if ($diff < $min) {
            $min = $diff;
        }
    }

    // Second pass: every pair with that difference
    $pairs = [];
    for ($i = 1; $i < $n; $i++) {
        if ($arr[$i] - $arr[$i - 1] == $min) {
            $pairs[] = [$arr[$i - 1], $arr[$i]];
        }
    }
    return $pairs;
}

print_r(closestNumbers([6, 2, 4, 10]));
// Pairs: [2, 4] and [4, 6]

Two passes over n − 1 neighbors each, plus the sort(). No comparing everything against everything.

Watch one tiny detail: the second pass uses ==, not =. Write if ($diff = $min) and you're assigning, not comparing, and the condition is almost always true. It's one of those one-character bugs that eats ten minutes.

This code returns the pairs as two-number arrays. How your challenge wants them delivered (a flat array, space-separated text, one pair per line) is set by the template and the sample output, not by me. Adjust only the end.

Step 6: the output format

This is the step where a correct solution can still fail. The output is compared character by character against the expected one: spaces, line breaks and order. If the example shows the numbers on one line separated by single spaces and you print one pair per line, the result is "wrong", even though the pairs are right.

So before submitting:

  • Run your code on the example from the statement.
  • Put your output next to the expected one and compare them character by character.
  • Make sure there's no trailing space and no missing line break.
  • Check the order: since the list is already sorted, pairs come out smallest to largest, but confirm that's what the statement asks for.

Why those five minutes pay off

In a 150-minute challenge, five minutes per problem feels like a luxury. With four problems that's twenty minutes without writing code, and when you watch the timer going down, the urge is to start right now.

But look at what they save you. Reading the constraints keeps you from writing a full solution you'll throw away after a timeout. Working the example by hand hands you the algorithm. The sentence pulls you out when you get lost. Checking the format keeps you from losing points over a space. Each of those mistakes, found halfway through, costs more than the step that prevents it.

It helps with nerves, too. When you have a routine, the first part of every problem is mechanical, and mechanical is calming. I wrote more about that in staying calm when the clock is running. And if you want to know which order to tackle problems in once you've read them, there's do the easy one first, then level up.

What I wouldn't do

  • I wouldn't start typing with the first idea. The first idea is almost always brute force, and with 100,000 items brute force doesn't make it.
  • I wouldn't write my own sorting algorithm in a challenge. sort() already exists, it's tested, and it's faster than any bubble sort I'd write in a hurry.
  • I wouldn't skip the hand-worked example "because it looks easy". That's where the detail that changes everything shows up, like the fact that it's every tied pair and not just one.
  • I wouldn't submit without comparing my output against the example's. The logic can be perfect and the result still fail over a space.
  • I wouldn't assume the output format. Not for this problem, not for any: the statement tells you.

Want to talk?

I do full web development, plugins and custom systems, servers and cloud. The same way of working, understanding the problem before touching the code, is how I work with clients. If you have a project or want to chat about this post, reach me through contact. And if coding challenges are your thing, I collect what I've learned from each one in hackathons.

Sources

  • 4-problem, 150-minute challenge, the Closest Numbers problem, switching from C to PHP and the 6-step routine: Samuel's own experience at the Google Core Hackathon End of Year (HackerRank, Nov 21, 2026).
  • Comparison counts for bubble sort and "everything against everything" with n = 100,000 (n²/2 ≈ 5 billion) and n·log₂ n ≈ 1.7 million: my own calculation, explained in the text.
  • PHP's sort() function — php.net

About the author

Samuel Torres is a Software & Cloud Engineer at Conqr Digital Agency (Mexico City). He runs 23+ production WordPress sites and processed 80,000 documents with OCR + LLM. More at /en/about. Get a quote on WhatsApp in 2 min

Samuel Torres Pichardo

Samuel Torres Pichardo
Software and cloud engineer in Mexico City, with about 3 years of professional experience.
About me · LinkedIn · GitHub · Contact

Got a project in mind?

Hire me: I'll help you build it.

Websites, online stores, custom systems and cloud. Tell me what you need and I'll tell you how I'd build it, what it costs and how long it takes.

  • Mexico City · remote

Menu

Español

[email protected]