Skip to content

Contact

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

Professional networks

LinkedIn · GitHub · Email
Applied engineering, measurable results.

Use what you already learned

· 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, 150 minutes to solve them. Official results aren't out yet (they take at least a week), so there's no final ranking to show off here. What I do have is something I noticed going back over my solutions: what got me through the last problems was what I had already used in the first ones.

Every problem you solve leaves you a tool. On the next one, the first question isn't "how do I solve this from scratch?" but "which of the tools I already have works here?".

I started the challenge in C and switched to PHP, which is what I use every day for WordPress and plugins. Python is faster to type, but I think in PHP. All the code here is PHP.

What I reused

These were the four problems and what each one left me for the next:

ProblemToolWhere I used it again
Closest NumbersSort before comparing (sort())The base for all the others
Ancestral NamesSort with my own rules (usort() + strcmp())Any time the order isn't the "normal" one
Minimum HealthSort and pick by positionThe opponent at position rank
Maximum EarningsSort several arrays together (array_multisort())To set up the dynamic programming table

Look at the middle column: all four start with sorting. Once the data is in order, a lot of problems turn into "compare with your neighbor" or "take whatever sits at position k".

In Closest Numbers that was literal: sort, then compare neighbors only. A bubble sort over 100,000 values does around 5 billion comparisons (n²/2 = 100,000 × 100,000 / 2) and won't finish in time, so I swapped it for sort() and two passes: find $min, then print the pairs with that difference. How I got to that idea before writing code is in first steps before writing a line of code.

Ancestral Names: sorting by your own rules

Ancestral Names was about sorting names that carry a Roman numeral, something like "Mary XIII" (that example is mine). Alphabetically by name first, and when the name is the same, by the numeral's value. The catch: as text, "XL" comes before "XVI", but XL is 40 and XVI is 16. A plain sort() won't do.

I split it into two tools:

  1. Split the text with explode(" ", $item): the name ends up at index 0 and the numeral at index 1.
  2. Convert the numeral to a number with the full table: I=1, V=5, X=10, L=50, C=100, D=500, M=1000. The rule: if a letter is worth less than the next one, subtract it; otherwise, add it. IX = -1 + 10 = 9.

Then usort with a compare function: strcmp for the name and a subtraction of values for the numeral.

<?php
function roman($r) {
    $v = ['I' => 1, 'V' => 5, 'X' => 10, 'L' => 50, 'C' => 100, 'D' => 500, 'M' => 1000];
    $total = 0;
    for ($i = 0; $i < strlen($r); $i++) {
        $current = $v[$r[$i]];
        if ($i + 1 < strlen($r) && $current < $v[$r[$i + 1]]) {
            $total -= $current;
        } else {
            $total += $current;
        }
    }
    return $total;
}

function sortNames($names) {
    usort($names, function ($a, $b) {
        $pa = explode(" ", $a);
        $pb = explode(" ", $b);
        $c = strcmp($pa[0], $pb[0]);
        if ($c != 0) {
            return $c;
        }
        return roman($pa[1]) - roman($pb[1]);
    });
    return $names;
}

That way "Steven XVI" lands before "Steven XL", because 16 is less than 40.

The usort callback returns a negative number if $a goes first, a positive one if it goes after, and zero if they're equal. strcmp already returns exactly that for text, and the subtraction returns it for numbers. That's the tool I keep: it handles any odd ordering, like by date and then by price.

The one-character bug

On one attempt only 1 of 10 test cases passed. The logic was fine. The problem: on one line I typed $pa[o], with the letter o, instead of $pa[0], with the digit zero. On screen they look almost the same.

The misleading part was that one case did pass: it had identical names, so it never reached that line. That sent me looking for a logic bug. It was a single letter.

That lesson is reusable too: when almost everything fails and one case passes, ask what's different about that one. It often tells you which line never ran. In the end Ancestral Names passed every test case.

Maximum Earnings: sorting three arrays at once

The last problem was dynamic programming. There are passengers; each one gets on at one point, gets off at another and leaves an amount. You can't carry two at the same time, and you want the highest total.

Why "always take the best payer" fails

The temptation is to grab whoever pays the most. Here's why that breaks:

  • X rides from 0 to 10 and leaves 12.
  • Y rides from 0 to 5 and leaves 8.
  • Z rides from 5 to 10 and leaves 8.

Take X and neither Y nor Z fits: you earn 12. Take Y + Z and you earn 16. The best move at each step doesn't give the best total, which is why you need a table.

The tool I already had: sorting

Before the table, passengers get sorted by where they get off. The data came in three arrays ($pickup, $drop, $tip); sort only one and you lose track of which pickup and which tip went with each drop.

array_multisort($drop, $pickup, $tip) sorts all three arrays together, using $drop as the guide. Same idea as Closest Numbers, but keeping the rows together.

The table, row by row

Maximum Earnings: best running total after each passenger, up to 20
Maximum Earnings: best running total after each passenger, up to 20

With passengers sorted by drop point, each one has two options: skip them (keep the best so far) or take them (what they leave plus the best I had before they got on). Write down the larger one.

PassengerRideLeavesSkipTakeWrite down
A0 → 56066
B2 → 99699
C9 → 11595 + 9 = 1414
D10 → 113143 + 9 = 1214
E11 → 145145 + 14 = 1919
F12 → 176196 + 14 = 2020

The answer is 20. Row D: they get on at 10, the last passenger off before that is B (at 9), so taking D gives 3 + 9 = 12; 14 is bigger, so I keep 14.

In code:

<?php
function maxEarnings($pickup, $drop, $tip) {
    array_multisort($drop, $pickup, $tip);
    $n = count($drop);
    $best = [0]; // $best[$i] = the most you can earn with the first $i passengers
    for ($i = 0; $i < $n; $i++) {
        // how many passengers are already off when this one gets on
        $j = 0;
        while ($j < $i && $drop[$j] <= $pickup[$i]) {
            $j++;
        }
        $take = $tip[$i] + $best[$j];
        $best[$i + 1] = max($best[$i], $take);
    }
    return $best[$n];
}

With the table's data it returns 20, and with X, Y and Z it returns 16, not 12. The while scans from the start every time; with large inputs I'd swap it for a binary search, since $drop is already sorted.

I submitted this one in the last few minutes and didn't get to confirm the result. What you see here is the idea reviewed calmly afterwards, not what happened on the platform.

My PHP toolbox

This is the list I walked away with. Pattern on the left, tool on the right:

PatternPHP tool
Sortingsort()
Custom comparisonusort() + strcmp()
The K strongestSplMinHeap
Sorting several arrays togetherarray_multisort()
Splitting textexplode()

It's short on purpose: five tools you know well beat twenty you have to look up in the manual while the clock runs. If you're getting ready for a challenge, building your own version of this table is part of preparing for a programming hackathon.

The key question: have I done something like this before?

In Minimum Health, a new player shows up at every level and Charlie fights whoever holds position rank among the strongest; you add up each opponent's strength. Reading it, I asked myself: have I done something like this before? I had. "Find the opponent at position rank" was the same as sorting and taking a position, something I'd already done twice that day.

<?php
$list[] = $newPlayer;
sort($list);
$opponent = $list[count($list) - $rank];

That version is correct and passed 11 of 15 test cases; the other 4 were timeouts (the clock icon means time limit, not a wrong answer). The fast version uses SplMinHeap holding only the rank strongest: top() is the opponent, with no full sort each time. Why I went for the working version first and left the fast one as an upgrade is in do the easy one first, then level up.

I didn't start by thinking about heaps. I asked what I already had, used it, locked in test cases, and only then thought about improving it.

What I wouldn't do

  • I wouldn't switch to a language I don't know well "to go faster". I switched from C to PHP, and it was to move to the one I use every day, not to one that just looks shorter.
  • I wouldn't solve each problem from scratch without first asking whether it looks like something I already did that same day.
  • I wouldn't trust the single passing case. If 1 of 10 passes, that case may be skipping exactly the line with the bug.
  • I wouldn't sort just one array when the data comes in several that belong together. That's what array_multisort() is for.

Want to talk?

If you're getting ready for a challenge like this, or you have a PHP or WordPress project, get in touch through contact. You can also see my other challenges under hackathons.

Sources

  • The four problems, the reused-tools table, the $pa[o] bug, the 11 of 15 test cases in Minimum Health, the Maximum Earnings table and the X, Y and Z example: Samuel's own experience at the Google Core Hackathon End of Year (HackerRank, Nov 21, 2026).
  • ~5 billion comparisons with bubble sort: my own calculation, n²/2 with n = 100,000.
  • sort() — php.net
  • usort() — php.net
  • strcmp() — php.net
  • array_multisort() — php.net
  • explode() — php.net
  • SplMinHeap — 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]