Explore Library
Code QuizAdvanced

Beam Search Keeps Worst Nodes

Beam pruning sorts the wrong way and keeps the lowest-scoring candidates.

Codejavascript
// Higher score = better; keep the top k candidates each step
function beamStep(candidates, k, score) {
  candidates.sort((a, b) => score(a) - score(b));
  return candidates.slice(0, k);
}

The beam retains the worst candidates instead of the best. What is the bug?