KatInterview

Randomly Fill Ad Slots Under Per-User View Caps

Arrays & HashingModel answer
MediumModel answerArrays & Hashingrandomizationhash-mapshuffle

Last updated

Randomly assign eligible ads to page positions for one user, respecting per-user display caps, avoiding repeats when possible, and returning the updated view counts.

Ad slot filling for a single user

A page has several named slots (for example "top", "sidebar", "bottom"). You need to decide which ad appears in each slot for one particular user, while respecting how often that user may see each ad.

Inputs

  • positions: a list of slot names.
  • candidateAds: a list of objects { adId: string, perUserLimit: number }. adId is unique, and perUserLimit is the most times this ad may ever be shown to this user.
  • userAdState: a map from adId to the number of times that ad has already been shown to this user. An ad missing from the map has been shown 0 times.

Task

Write a function that returns a pair: (assignments, updatedUserAdState). assignments maps every position to an adId, or to null if it could not be filled.

Rules

  1. Choose among eligible ads at random.
  2. An ad is eligible only if its current count is strictly less than its perUserLimit.
  3. If the number of eligible ads is at least the number of positions, every position must get a different ad within this call.
  4. If there are fewer eligible ads than positions, fill as many positions as you can. An ad may appear in more than one position in this case, but only while its remaining allowance (limit minus count, including views granted earlier in this same call) stays above zero.
  5. Each placement increases that ad's count in the state by one.
  6. Return both the assignment and the updated state.

Example

positions = ["top", "middle", "bottom"]

candidateAds = [
  { adId: "ad_a", perUserLimit: 2 },
  { adId: "ad_b", perUserLimit: 1 },
  { adId: "ad_c", perUserLimit: 3 }
]

userAdState = { "ad_a": 1, "ad_b": 1 }

ad_b is exhausted. ad_a has 1 view left and ad_c has 3. One valid result is:

assignments = { top: "ad_c", middle: "ad_a", bottom: "ad_c" }
updatedUserAdState = { ad_a: 2, ad_b: 1, ad_c: 2 }

Because selection is random, other valid outputs exist (for example, ad_a and ad_c swapping places). Note that a stricter reading of rule 4 ("never repeat an ad within one call") would leave bottom as null. Say which interpretation you are implementing and why; this write-up follows the rules as written, where filling more positions wins.

Expected from you

  • A working implementation, with a short justification of correctness.
  • Time and space complexity.
  • Handling of edge cases (empty inputs, bad limits, duplicate ids).
  • A brief discussion of how you would make it testable and how it would behave under concurrent requests for the same user.

Hints

Hint 1

Convert each eligible ad into a remaining allowance (limit minus current count) and drop anything at zero. Everything after that can be driven by these allowances rather than by re-checking the original counts.

Hint 2

Split the work in two passes: first give out distinct ads by shuffling the eligible list and pairing it with the positions; only if positions are still empty, move on to a second pass that is allowed to repeat ads.

Hint 3

In the repeat pass, keep a pool of ads that still have allowance, pick a random one, decrement its allowance, and remove it from the pool in O(1) by swapping it with the last element and popping when it reaches zero.