← 返回 bytedance 的题目列表Random Sample from Unknown Size Dataset
类型:online_judge
bytedance
Given a dataset of unknown size, you have access to a function that can sample one item at a time without repetition until all items are sampled. Design an algorithm to randomly select n samples from this dataset, ensuring each sample has an equal probability of being chosen. The dataset's size is unknown, and n is less than or equal to the dataset size. Provide the code implementing this algorithm and at least 5 test cases.
Example
Input
function that simulates a stream of numbers 1 to 1000