Insert Delete GetRandom O(1) variant: given two values a and b representing a range [a,b], return a random value weighted by the value itself (e.g. a=1, b=3 gives prob = [1, 2, 2, 3, 3, 3]).
Asked in the Onsite - Coding design/modularity stage. Round focused on design and modularity.
Implement weighted_random(a, b). For deterministic grading, return an ascending array containing each distinct integer that can be selected from the inclusive range. The sampling model assigns weight equal to the integer value, so value x has probability x / sum(a..b). Assume 1 <= a <= b.
def weighted_random(a, b):