Senior · Trace · Ordering correctness

Trace the complete program for test comparator laws instead of subtracting. State the exact stdout, then justify the state transitions and relevant edge conditions.

Think through your answer before opening the explanation.

Coding interviews

using System;
using System.Linq;
using System.Collections.Generic;
using System.Globalization;
using System.Text;

int[] values={int.MinValue,-1,0,1,int.MaxValue}; int checks=0;
foreach(int a in values) foreach(int b in values) {
    if(Math.Sign(a.CompareTo(b))!=-Math.Sign(b.CompareTo(a))) throw new Exception("antisymmetry"); checks++;
    foreach(int c in values) { if(a.CompareTo(b)<=0 && b.CompareTo(c)<=0 && a.CompareTo(c)>0) throw new Exception("transitivity"); checks++; }
}
Console.WriteLine("law checks:"+checks);
Console.WriteLine("subtraction wrong:"+(unchecked(int.MinValue-int.MaxValue)>0).ToString().ToLowerInvariant());
Console.WriteLine("safe sign:"+Math.Sign(int.MinValue.CompareTo(int.MaxValue)));
Reveal answer and reasoning

The output lines, in order, are “law checks:150”; “subtraction wrong:true”; “safe sign:-1”. There are twenty-five ordered pairs, each checking sign reversal, and one hundred twenty-five ordered triples checking the nondecreasing transitivity implication. That totals one hundred fifty checks. Unchecked minimum minus maximum wraps to a positive value, contradicting numeric order. CompareTo returns the correct negative sign for the same pair without overflowing subtraction.

The triple loop increments the check counter for every triple even when the implication's premises are false. Therefore the count is twenty-five pair checks plus one hundred twenty-five implication checks, not the number of times the premise held. The subtraction line is explicitly unchecked so its wraparound is part of the demonstrated counterexample.

The explicit law harness takes O(k cubed) time for k selected values and constant auxiliary state, which is appropriate for a small diagnostic fixture rather than a sorting algorithm. Passing these finite tests is evidence, not a proof for every custom type or combination of nullable values. Using x minus y, inconsistent null placement or culture-dependent text rules can violate an intended ordering contract.

A comparator must also agree with equality classes expected by the consuming collection. Random examples alone can miss boundary failures, so retain extremes and equal values.

What a strong answer covers

  • Exact output: law checks:150 | subtraction wrong:true | safe sign:-1
  • There are twenty-five ordered pairs, each checking sign reversal, and one hundred twenty-five ordered triples checking the nondecreasing transitivity implication.
  • Using x minus y, inconsistent null placement or culture-dependent text rules can violate an intended ordering contract.

Common pitfall

Using x minus y, inconsistent null placement or culture-dependent text rules can violate an intended ordering contract. A comparator must also agree with equality classes expected by the consuming collection. Random examples alone can miss boundary failures, so retain extremes and equal values.

Follow-up questions

  1. Create a nontransitive cyclic order for three named values and make the triple checks fail. Hint: a compares before b, b before c and c before a cannot form an ordinary total order.
  2. What fails if a key used by an ordered collection mutates after insertion?

Self-review

Choose what to revisit. These marks are your own assessment and are separate from lesson completion.

Primary references

Open lesson and code ↗

← All interview questions