This question evaluates probabilistic sampling, numerical stability, and algorithmic implementation skills for weighted selection under floating-point edge cases, and is categorized in Coding & Algorithms for a Machine Learning Engineer role.
You are given:
texts
(e.g., user comments), length
n
.
scores
of length
n
, where each score can be any real value, including
-∞
or
+∞
.
Design an algorithm/function that randomly returns one string from texts such that the probability of returning texts[i] is proportional to the score distribution.
Define the sampling rule explicitly as follows:
i
with probability
+∞
, return one of the
+∞
items uniformly at random.
-∞
(so all weights are zero), return any item uniformly at random.
Requirements: