230. There is a linked list of numbers of length N. N is ... .....
Microsoft Interview Questions and Answers
(Continued from previous question...)
230. There is a linked list of numbers of length N. N is ... .....
Question:
There is a linked list of numbers of length N. N is very large and you don't know N. You have to write a function that will return k random numbers from the list. Numbers should be completely random.
maybe an answer:
C# version
Node f(Node list)
{
if(list==null) return null;
Node p = list;
int i=0;
while(true)
{
if(p.next==null)
{
return p;
}
Random rand = new Random(0,1);
if(rand.next()<1/(i+1))
{
return p;
}
else
{
p=p.next;
i++;
}
}
}
(Continued on next question...)
Other Interview Questions
|