The pseudocode-first constraint sounds easy but it actually slowed me down because I kept wanting to just write code.
Start by clarifying the sampling requirements: is the data streamed or static, is the sample size fixed, and are weights involved? Then outline the appropriate algorithm (reservoir, weighted, or rejection) in pseudocode, discussing time/space complexity and trade-offs. Finally, provide a clean implementation in a language of your choice, handling edge cases and explaining your design choices.
Pro tip: Explicitly state the assumptions you're making (e.g., uniform random number generator available, stream length unknown) and how they affect your choice. This shows you think about real-world constraints and not just the algorithm.
Ask questions to determine the sampling variant: Is the data a stream or a static array? Is the sample size fixed? Are there weights? What are memory constraints?
Based on requirements, select reservoir sampling (uniform from stream), weighted sampling (e.g., A-Res, A-ExpJ), or rejection sampling (for specific distributions). Justify your choice.
Write clear pseudocode for the chosen algorithm, explaining each step and the role of randomness. Mention time and space complexity.
Provide a full implementation in a suitable language, handling edge cases (e.g., empty stream, sample size larger than stream). Briefly discuss testing strategy.
Compare alternatives: e.g., reservoir vs. rejection for uniform sampling, or weighted reservoir vs. precomputing cumulative weights. Mention scalability and precision issues.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.