Single-Rule Ad Filter
Problem statement
Filter a list of ads using one rule and return the matching IDs in their original input order. An ad has an ID, an integer viewer age and a set of target locations.
The parallel arrays ids, ages and adLocations describe the ads: row i is (ids[i], ages[i], adLocations[i]). The parameter mode carries the rule’s AND/OR operator. The rule has ruleLocations, minAge, maxAge and mode. For this exercise, -1 represents a missing age bound and an empty rule location list means no location criterion.
- The age criterion is active if at least one bound is present. It passes only if the age satisfies every present bound, inclusively. The two bounds form one criterion even for OR.
- The location criterion is active when ruleLocations is nonempty. It passes if the ad and rule share at least one location, ignoring ASCII letter case. An ad with no locations fails an active location criterion.
- AND keeps an ad when every active criterion passes. OR keeps it when at least one active criterion passes. If neither criterion is active, keep every ad for either mode.
For this exercise, the model’s age field is used for both bounds; the source example’s minage label is treated as that same ad age. The grouping, case comparison, empty-criterion rules, parallel-array representation and bounds above are explicit practice choices.
Function
filterAds(ids: String[], ages: int[], adLocations: String[][], ruleLocations: String[], minAge: int, maxAge: int, mode: String) → String[]Examples
Example 1
ids = ["A","B","C"]ages = [21,13,25]adLocations = [["US","CA"],["US"],["CA"]]ruleLocations = ["US"]minAge = 21maxAge = -1mode = "AND"return = ["A"]A satisfies age at least 21 and shares US. B shares US but its age is too small. C satisfies the age bound but shares no rule location. AND therefore returns ["A"].
Example 2
ids = ["D","E","F"]ages = [25,20,35]adLocations = [["TX"],["ca"],["TX"]]ruleLocations = ["CA"]minAge = 30maxAge = -1mode = "OR"return = ["E","F"]D fails both active criteria. E shares CA when case is ignored, and F satisfies the age bound. OR returns ["E","F"] in input order.
Constraints
0 <= ids.length <= 2000. The three ad arrays have equal lengths.- IDs are distinct nonempty strings of at most 20 ASCII letters or digits.
0 <= ages[i] <= 1200 <= adLocations[i].length <= 20and0 <= ruleLocations.length <= 20- Each location code has one to three ASCII letters. Location lists contain no case-insensitive duplicates.
-1 <= minAge <= 120and-1 <= maxAge <= 120. When both are present, minAge <= maxAge.- mode is exactly AND or OR.