Code RoomNumerically stable softmax
MediumPrep Room Coding #1541

Numerically stable softmax

CodingML systemsMid–Senior~18 min

Implement a numerically stable softmax over a non-empty list of real-valued logits. Softmax(x)_i = exp(x_i) / sum_j exp(x_j). To avoid overflow on large logits, subtract the maximum logit from every element before exponentiating (this does not change the result mathematically). Return the list of probabilities, each rounded to 6 decimal places.

Implement
softmax(logits: list[float]) → list[float]
Examples
in[[0,0]]out[0.5,0.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.

0:00 of about 18 min
InputExpectedGot
[[0,0]][0.5,0.5]not run yetsample