Halve the Search

In the last exercise, git log -S weekly-report took you straight to the commit that renamed npm run report. That worked because package.json gave you a script name to search for. Plenty of bugs give you no such handle. A value comes out wrong at the end of a long path, a big batch of input fails somewhere in the middle, or something that worked at v1.0 fails now and nobody knows which file changed.

This lesson teaches one strategy for all three: make every check rule out half of what’s left. You’ll use it to find where a value goes wrong in a handful of checks and to decide what to halve when a new bug arrives. Start with the counting, since it’s the reason the strategy is worth learning.

Count the Checks

Checking every step in order feels careful, but on a long search it’s slow, and people give up on it halfway. A toy example shows why. The Second Draft has no order import you can run on the staff desk, so imagine one: Grace pastes in 1,024 orders from an old spreadsheet, the import crashes, and the error doesn’t say which order caused it.

Before you read on, decide on a number: at most, how many runs of the import would it take you to find the one bad order?

Checking one order at a time could take over a thousand runs. Halving takes at most ten.

Run the import with the first 512 orders only. If it crashes, the bad order is among those 512.

If it doesn’t, it’s among the other 512. Either way, one run has ruled out half. Then halve what’s left: 256, 128, 64, 32, 16, 8, 4, 2, and finally 1. That’s ten runs.

You can confirm that count, and see how it grows, with a short program that halves a number of items until one is left:

Language: JavaScript
function countHalvings(items) {
    let left = items;
    let checks = 0;
    while (left > 1) {
        left = Math.ceil(left / 2);
        checks += 1;
    }
    return checks;
}

console.log("1,024 orders:", countHalvings(1024));
console.log("2,048 orders:", countHalvings(2048));
console.log("300 commits:", countHalvings(300));
console.log("6 stops on a path:", countHalvings(6));

The while loop keeps halving until one item is left. Math.ceil() rounds up because an odd number splits into unequal halves, and in the worst case the fault sits in the bigger one. Run it, and it prints how many checks each search needs:

Language: Program Output
1,024 orders: 10
2,048 orders: 11
300 commits: 9
6 stops on a path: 3

Doubling the orders adds only one check, so halving stays practical on searches you’d never finish one step at a time.

To feel that count rather than read it, run Grace’s import yourself. Each click on Run orders runs it on the first half of the orders still suspect and keeps whichever half holds the bad order. Click until one order is left, then try New import to hide the bad order somewhere else:

Interactive diagram — enable JavaScript to view.

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Become a Member to join the conversation.