Compute ROC AUC without a library. Given labels (1 for a positive, 0 for a negative) and the model's scores, write auc(labels, scores): the probability that a randomly chosen positive gets a higher score than a randomly chosen negative, where a tie counts as half. Round to 4 decimal places, and return None if there are no positives or no negatives. It should handle tens of thousands of examples comfortably.
Examples
auc([1, 0, 1, 0], [0.9, 0.1, 0.8, 0.4])
1.0
auc([1, 0, 1, 0, 1], [0.7, 0.6, 0.6, 0.2, 0.3])
0.75
What this question tests
Tests ROC AUC from its probabilistic definition, with ties, at a scale where comparing every positive with every negative is too slow. Writing AUC by hand is a well-known question in ranking and recommendation interviews, and the sort-based answer is the one that shows you understand it.
More Python interview questions