Code Room
CodingMediumcod-g983
Subject Ml metricsLevel Mid–Senior~18 minCommon in ML systems · Algorithms & data structures interviewsIndustries Software development, Technology

Question

Compute the Gini impurity of a list of class labels (integers or strings). Gini impurity = 1 - sum over each class of (p_class)^2, where p_class is that class's fraction of the labels. A pure node (all one class) has impurity 0. The input list is non-empty. Round the result to 6 decimal places.

Implement
gini_impurity(labels: list[int]) → float
Examples
in[[0,0,1,1]]out0.5
What a strong answer looks like

State your approach and its time/space complexity out loud before you optimize. Handle the edge cases (empty input, duplicates, overflow), and say why you chose this over the brute force. Green tests are the floor, not the grade.

Vibe coding: describe the solution in plain language (or narrate it) and the coach grades your approach. Generating runnable code from your description is coming next.

Run or narrate your approach, then ask the coach.