AlgoFrames

Spot the bug: equal keys swap order

Standard · Find the bug · about 8 min

This counting sort orders [key, id] pairs by key, but pairs with equal keys come out in reverse order. Click the line with the bug, then fix it.

Example 1

Input:

[[[1,1],[1,2]],1]

Output:

[[1,1],[1,2]]

The buggy code returns [[1, 2], [1, 1]].

Constraints

  • 0 ≤ pairs.length ≤ 1,000
  • 0 ≤ key ≤ k ≤ 100

Learn the idea first: Counting sort, a visual lesson.

More Counting sort problems