build-a-spell-checker / lesson-22.md
Lesson 22 · Ranking corrections

Ranking a candidate set

Given a handful of candidate words, the checker must pick the best one. Today you rank a candidate set by frequency, breaking ties alphabetically so the choice is always deterministic.

The goal

Choose the highest-frequency word from a set of candidates, with alphabetical order breaking ties.

Start here - the target
TO DO
Scenario: Picking the most likely candidate
Givena dictionary with counts the:1000, ten:100, tea:100
WhenBestByFreq(["tea", "ten", "the"]) is called
Thenit returns "the" (the highest count)
AndBestByFreq(["ten", "tea"]) returns "tea", because the counts tie at 100 and "tea" sorts first
Background

Candidate generation hands you a set of plausible words; ranking turns that set into a single answer. The rule is Norvig’s: prefer the most frequent candidate, because a common word is a likelier intended target than a rare one. Among tea, ten, and the, the count of the towers over the others, so it wins.

The tie-break matters more than it looks. When two candidates share the top count, picking “whichever came first” makes the answer depend on the order the generator happened to produce - flaky and unrepeatable. Breaking ties alphabetically makes BestByFreq a pure function of the candidates and their counts, so the same input always yields the same correction. Determinism like this is what lets every later lesson pin an exact expected suggestion.

Make it work
func (d *Dictionary) BestByFreq(cands []string) string {
// return the candidate with the greatest Count; on a tie,
// prefer the alphabetically smaller word so the result is
// deterministic (never depends on candidate order)
}
CheckpointDONE
You can pick the most likely word from a candidate set. Commit and stop here.