Job Turned Brain Teaser

Job Turned Brain Teaser

I’ve been writing software professionally for 26 years, and until last week I had never attempted to solve a LeetCode problem.

If you haven’t heard of it, LeetCode is a website with thousands of programming puzzles. Each one is a small, self-contained problem, like “find the middle value of two sorted lists of numbers.” You write a solution in the browser, and the site runs it against a hidden set of test cases and tells you whether it’s right and how fast it is compared to other submissions. For years it was where engineers went to cram for job interviews, because a lot of tech companies tested candidates with exactly these puzzles.

I’m not interviewing for anything. I signed up because a code puzzle sounded like a fun way to keep my brain sharp. In the spirit of that, I wrote every line of code myself. I picked a Hard problem as my first exercise, and it turned into a week-long experiment that confirmed my thoughts on what coding is now.

The problem

The puzzle was “Median of Two Sorted Arrays.” You get two lists of numbers, each already sorted, and you return the median of everything combined. Each list holds up to 1,000 numbers, and the values run from -1,000,000 to 1,000,000.

That part is easy. The catch is one line in the problem statement: the solution should run in O(log(m+n)) time.

In plain terms, you aren’t allowed to look at every number. Each step has to throw away a big chunk of what’s left, the way you’d find a name in a phone book by opening to the middle and discarding half. The obvious approach, combining the lists and picking the middle, is off the table.

Attempt one: confident and wrong

My first idea was to search over the values instead of the positions. Guess a number, count how many elements sit on each side of it in both lists, and move the guess toward the heavy side until the counts balance.

It took me about five hours to get working. It passed LeetCode’s examples, my own test cases, and a 2,000-element stress case that ran instantly. I was pretty pleased with myself.

Then I had Claude fuzz test it. Fuzz testing means throwing huge numbers of random inputs at your code and comparing every answer against a dumb but obviously correct version. Out of a couple hundred thousand random cases, about 21% failed. Every failure involved duplicate values. With [4] and [3,3], my code returned 3.5 when the answer is 3.

So I patched it. The first patch got failures down to 8%. The second fixed every case I targeted and pushed failures back up to 14%. That’s when I stopped. Every patch was guessing about a situation the core logic should never have produced. I had four rescue branches wrapped around one wrong idea: my test asked “are the counts on each side equal?”, and with duplicates that question often has no exact yes. I’d been patching complexity on top of a structural problem, and I’ve watched enough real projects do that to know how it ends. I scrapped it.

Attempt two: the ties fight back

The second design started by taking an actual element from near the middle of each list as a candidate, binary search where it lands in the other list, count what’s on each side, and narrow toward the heavy side until one candidate splits everything evenly. This turned out to be a much better structural base for the code, didn’t require tracking information across iterations, and didn’t require rescue branches.

My fuzz-and-fix loop looked like this:

RoundCases failingWhat it found
113%A swapped-limits bug, searches that never finished, crashes
29%A shortcut I broke by reordering code
35%Ties: many copies of the same value in the middle
452 of 204,000Only large, tie-heavy arrays, so I added a medium-size tier, which found 5,247 more
516, plus 960 in the new tierOne convergence problem around long runs of a single value
60Nothing

The best bug of the week was in round 1. I’d initialized each candidate’s search limits from the other list’s length. One character in the wrong place, and it passed tons of tests anyway, because whenever the two lists happen to be the same length, the bug disappears.

The most expensive lesson was a safety net. I’d capped my main loop and recursive call, and quietly returned 0 if they ran out. I spent two hours chasing what I thought was a logic bug before realizing the cap had fired. I knew better than to introduce silent failures, and changed every safety net to throw an exception. From then on every failure told me exactly where it happened.

Two other things made the development loop fast. The fuzzer started shrinking failures automatically, deleting elements until it found the smallest input that still failed, so a 600-element failure became 12 numbers I could trace in the debugger. And every failure became a permanent unit test. I finished with 42 of them.

The final version passed 385,000 checks, including every possible pair of sorted lists up to six long using the values 0 through 3. Then I submitted it to LeetCode: accepted on the first try, 2,099 of 2,099 test cases passed.

Three solutions, one scoreboard

Once my efficient solution was done, I wrote the version I would actually ship at work: copy both lists into one array, sort it, take the middle. It took 15 minutes and passes all the same tests. Then I asked Claude for its own solution. It wrote the textbook approach in seconds, and that code passed the same 204,000-case fuzz test on its first run.

My efficient solution
My practical real-world solution
Claude’s textbook solution

I benchmarked all three on the largest allowed input, 1,000 numbers per list, averaged over 2,000 random cases:

SolutionTime to writeLines of codeTime per call
Real world: combine, sort, take the middle15 minutes3162 µs
Efficient artisanal: mine20 hours About 2005.3 µs
AI: Claude’s textbook solutionSeconds230.08 µs

My version is about 12 times faster than the practical real-world one. That sounds great until you notice two things.

First, the AI version is 65 times faster than mine, in a ninth of the code, and it arrived before I finished reading the problem statement.

Second, all three are fast. The slowest takes 62 millionths of a second. My efficient version saves about 57 µs per call over the 15-minute version, so if my 20 hour week would need about 1.3 billion calls to earn that time back. LeetCode’s own scoreboard agreed it didn’t matter: my 4 ms runtime beat 49.5% of C# submissions, because at this size the timer is mostly measuring .NET starting up.

From job to brain teaser

That scoreboard is the whole argument. Hand-writing a clever algorithm used to be part of the job. Now it’s the slowest, least efficient way to get the result, by a wide margin.

Think about Stack Overflow. For fifteen years it was every developer’s messy tool bin. You showed up with an error message, grabbed the answer that worked, and left. Once an LLM could answer faster and fit the answer to your exact code, most of us stopped visiting without ever deciding to quit. Stack Overflow was purely a means to an end, so when a better means showed up, it faded.

LeetCode is different, because the puzzle is partly the point. Its job as an interview gatekeeper may already be eroding with companies increasingly allowing and even incorporating AI in live interviews. The algorithm-writing itself, the part we used to interview for, has become a brain teaser.

Catch and release

Was it worth a week? Absolutely, just not for the code. I got a hard, honest workout I don’t get from directing agents. I relearned the value of fuzz testing, how a silent safety net can hide a bug for hours, and the discipline to throw away a design instead of patching it forever.

None of those 200 lines will ever run in production. The 8-line version would beat them in any code review, and the AI version beats them on every benchmark. That’s fine. Think of it as catch-and-release programming: you land a solution, admire it for a minute, and throw it back.

I’ll be back for more of these puzzles. Maybe an Easy one next.