Explore Library
Code QuizAdvanced

Priority Queue Tie-Breaking

Find the comparator flaw that breaks ordering when priorities are equal.

Codejavascript
// Pop node with lowest f; break ties by lower insertion order (count)
function popBest(nodes) {
  nodes.sort((a, b) => {
    if (a.f !== b.f) return a.f - b.f;
    return b.count - a.count; // tie-break
  });
  return nodes.shift();
}

What is the bug in the tie-breaking logic?