Probability and Statistics based coding Questions

I recently got stumped by a couple of stats related coding questions and was hopping to see if someone here could answer them.

  1. You have a function that generates 1 of 5 distinct values. You can think of this as a 5 sided die. But you want to generate 1 of 7 distinct values with the same probability only using the function that generate one of 5 values.

My intuition for this one told me that using something like a mod (%) might be useful here but I couldn't figure out how to keep an even probability

  1. You have stream of web queries, it doesn't really matter what they are, and you want to remember a random sample of them of size k, and you have to keep the space complexity to size k + 1, (or not much larger than k).

I started using a queue of size k that you would populate until you see k queries. Once you have k+1 queries you have to decide if you are adding that query to your queue and then with some probability you have to decide which element is getting popped from your queue.

Let me know what you think of either of them, I've been tryong to wrap my head around these two all day.

Comments (2)