Filter Ads by a Single Rule
When a viewer starts a stream, the ad server filters its pool down to the ads that match one targeting rule.
Each ad is described by three parallel arrays: adIds[i] is the ad's id, adLocations[i] is the list of locations it targets (for example ["US", "CA"]) and adAges[i] is the viewer age it is meant for.
The rule is ruleLocations (a list of required locations; empty means the location constraint is absent), ruleMinAge and ruleMaxAge (-1 means that constraint is absent) and an operator, "AND" or "OR".
- The location constraint, when present, is satisfied when every required location is among the ad's locations. Compare locations case-insensitively.
- The minimum-age constraint, when present, is satisfied when
adAge >= ruleMinAge; the maximum-age one whenadAge <= ruleMaxAge. ANDrequires every present constraint;ORrequires at least one. An absent constraint is ignored, and when no constraint is present every ad matches.
Return the matching ad ids in their original order.
adIds = ["A", "B", "C"] adLocations = [["US", "CA"], ["US"], ["CA"]] adAges = [21, 13, 25] rule: ruleLocations = ["US"], ruleMinAge = 21, ruleMaxAge = -1 operator "AND" -> ["A"] (B fails the age, C the location) operator "OR" -> ["A", "B", "C"] (B passes location, C passes age)
The pool can be large; keep the work per ad proportional to its number of locations.
Hints
Hint 1
Build the list of present constraints first, each as a predicate over one ad. The operator then reduces to every or some over that list, and an empty list means every ad matches.
Hint 2
Normalise locations to one case once, and put the ad's locations in a Set so each required location is a constant-time lookup.
Topics
Asked at
Sign in to use the assistant — ask what the problem means, get a hint when you're stuck, or have your current work reviewed. Conversations save to your account.