LeetCode
Random Pick Index
Click
here
for an explanation.