Pick at Random from a Stream

Talk it through out loud with your AI coach.

The problem

  • medium
  • Pro

Items arrive one at a time from a stream whose length you don't know in advance, and it's far too long to store. When it ends, you must return one item chosen uniformly at random, so every item had an equal chance. Explain how, and prove that it's fair.

What a strong answer covers

  • A rule for when a new item replaces the current choice.
  • A short probability argument that every item ends up equally likely.
  • How the idea extends to choosing several items.
  • One pass and the memory needed.

Common mistake: Replacing the choice with a fixed probability, such as a half, which heavily favours the items near the end.

Knowing this is half of it. Saying it clearly out loud is the part the coach helps you practise.

Practise it out loud