Generate Weighted Random NFTs with Updates
Company: Coinbase
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Onsite
Quick Answer: This question evaluates understanding of weighted random sampling, efficient data structures for maintaining and updating prefix sums, probabilistic reasoning about sampling distributions, and reproducible testing of randomized algorithms within the Coding & Algorithms domain.
Constraints
- 1 <= k <= 10^5 (number of NFT types).
- 0 <= w[i]; at least one weight is positive at construction time.
- For each ["mint", r] op, 0 <= r < total (the current sum of weights), and total > 0.
- For each ["update", i, delta] op, 0 <= i < k and w[i] + delta >= 0.
- 0 <= len(ops) <= 2 * 10^5.
Examples
Input: ([1, 2, 3], [["mint", 0], ["mint", 1], ["mint", 2], ["mint", 3], ["mint", 5]])
Expected Output: [0, 1, 1, 2, 2]
Explanation: Intervals: idx0=[0,1), idx1=[1,3), idx2=[3,6). Targets 0,1,2,3,5 land in idx0, idx1, idx1, idx2, idx2 respectively.
Input: ([5], [["mint", 0], ["mint", 4]])
Expected Output: [0, 0]
Explanation: k=1 edge case: the single index owns [0,5), so every target returns 0.
Hints
- Index i should own the half-open cumulative interval [cum(i-1), cum(i)) of length w[i]; target r lands in it with probability w[i]/total. Use a strict '>' (first index whose cumulative sum exceeds r) so zero-weight items (empty intervals) are never returned.
- A static prefix-sum array gives O(log k) mint via binary search, but update is O(k) because changing w[i] shifts every later prefix. Replace it with a Fenwick / segment tree to get O(log k) updates.
- On a Fenwick tree you can find the owning index without materializing the prefix array: walk bits from the highest down; greedily take the largest jump whose subtree sum still fits under the remaining target, subtracting as you go. The index you stop at is the interval owner.