This one had way more surface area than I expected from a single question.
Start by clarifying requirements and edge cases, then outline a solution using cumulative distribution and binary search for O(log n) sampling. Implement the generator with input validation and floating-point tolerance, and discuss trade-offs between preprocessing and sampling efficiency. Finally, describe unit tests covering deterministic and statistical checks.
Pro tip: Mention that using a cumulative sum array with binary search (e.g., bisect) is efficient for large n, but for small n a linear scan may be faster due to cache locality. Also, highlight the importance of seeding for reproducibility in statistical tests.
Ask about input constraints (size, value range), whether probabilities are guaranteed to sum to 1, and how to handle floating-point errors. Discuss validation needs and expected performance.
Propose using cumulative probabilities and binary search for O(log n) sampling. Alternatively, mention alias method for O(1) but with higher preprocessing. Explain how to handle floating-point precision by normalizing or using tolerance.
Write a Python generator that validates inputs (lengths match, probabilities non-negative, sum to 1 within tolerance), precomputes cumulative sums, and yields values indefinitely using random.random() and bisect.
Compare O(n) preprocessing and O(log n) per sample for cumulative+binary search vs O(n) preprocessing and O(1) per sample for alias method. Discuss memory usage and suitability for different n.
Include tests for input validation (invalid probabilities, mismatched lengths), deterministic checks with a seeded RNG, and statistical tests (e.g., chi-square) to verify distribution over many samples.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.