Trace through w = [2, 5, 3].
Build prefix sums: [2, 7, 10]. Total weight is .
Call pickIndex(). Generate random number (between and ).
Binary search [2, 7, 10] for smallest value . Left , right . Mid , prefix[1] . Move right to . Left , mid , prefix[0] . Move left to . Left right . Return index .
Index owns the range [3, 7], which is out of values. So it gets picked % of the time.
time and space for initialization. time per pickIndex call.