Code Room
Code reviewHardcr-g260
Subject Comparator violationsLevel Senior–Staff~25 minCommon in Algorithms & data structures interviewsIndustries Software development

Question

Review this Java comparator used to sort tasks by priority then by an int weight.

Tasks come from an upstream service and weights/priorities can be any 32-bit int, including large magnitudes. The team reports sporadic `IllegalArgumentException: Comparison method violates its general contract!` from `TimSort`, but only on certain shards.

What a strong answer looks like

Separate real bugs from style. Rank issues by severity, point at the root cause rather than the symptom, and suggest a concrete fix — specific and kind.

Talk through your review
Code to reviewjava
class Task {    int priority;   // any 32-bit int from upstream    int weight;     // any 32-bit int from upstream} void schedule(List<Task> tasks) {    tasks.sort((a, b) -> {        if (a.priority != b.priority) {            return a.priority - b.priority;   // ascending by priority        }        return b.weight - a.weight;           // descending by weight    });    run(tasks);}
Run or narrate your approach, then ask the coach.