I went straight for the prefix-sum plus binary search approach.
Start by clarifying the problem: we need to select k distinct winners without replacement, with probability proportional to spending. Then present an efficient algorithm, such as using a Fenwick tree for weighted random selection, and analyze its time and space complexity.
Pro tip: Mention the alternative approach of exponential race (generating keys) and discuss trade-offs; this shows depth and awareness of different solutions.
Confirm that winners are selected without replacement and that each subset of size k has probability proportional to the product of spending of its members.
Use a Fenwick tree (Binary Indexed Tree) to store cumulative spending, enabling efficient updates and prefix sum queries.
For each of k iterations, generate a random number between 0 and total remaining spending, find the corresponding participant via binary search on the Fenwick tree, select them, and remove them by setting their spending to 0.
Explain that at each step, the probability of selecting a participant is proportional to their spending, and by induction, the overall selection is correct.
Building the tree takes O(n). Each selection and removal takes O(log n), so total time is O(n + k log n). Space is O(n).
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.