Filter ads by a single rule
Single-Rule Ad Filter
Problem Description
When a user watches content, the system needs to filter an ad pool to find ads that match a single rule with an operator. Implement a lightweight filter that supports this functionality.
Data Model
Ad
id: StringtargetLocations: Set<String>(e.g.,{ "CA", "TX" })age: int(intended viewer age for the ad)
Rule
targetLocations: Set<String>(optional)minAge: Integer(optional)maxAge: Integer(optional)operator: "AND" | "OR"
Matching Semantics
- Location match: If
rule.targetLocationsis present and non-empty, an ad matches the location constraint ifrule.targetLocations ⊆ ad.targetLocations(i.e., the ad targets all locations required by the rule). - Age match:
- If
rule.minAgeis present, requiread.age >= rule.minAge. - If
rule.maxAgeis present, requiread.age <= rule.maxAge.
- If
- Combine the (present) constraints using
rule.operator:AND: Ad must satisfy all present constraints.OR: Ad must satisfy at least one present constraint.
- If a constraint is absent, ignore it.
Input / Output
- Input:
List<Ad> ads,Rule rule - Output:
List<String>of matchingad.idvalues, preserving the original input order.
Example
Rule: minAge = 21, targetLocations = {"US"}, operator = AND
Ads:
A:age=21,targetLocations={"US","CA"}→ matchB:age=13,targetLocations={"US"}→ no (age)C:age=25,targetLocations={"CA"}→ no (location)
Output: ["A"]
Constraints
ads.lengthcan be large; keep it efficient.- Location strings may vary in case; clarify/handle consistently (e.g., normalize to upper-case) if needed.
AI interview prep
Core dilemma
Balancing efficient filtering of a potentially large ad pool with dynamic, conditional logic of a single rule, while strictly preserving the input order.
3 tradeoffs and 2 stress tests locked
Interview prep
Unlock the interviewer X-Ray rubric, tradeoff matrix, stress tests, clarifying questions, and key concepts.
Filter ads by a single rule
Single-Rule Ad Filter
Problem Description
When a user watches content, the system needs to filter an ad pool to find ads that match a single rule with an operator. Implement a lightweight filter that supports this functionality.
Data Model
Ad
id: StringtargetLocations: Set<String>(e.g.,{ "CA", "TX" })age: int(intended viewer age for the ad)
Rule
targetLocations: Set<String>(optional)minAge: Integer(optional)maxAge: Integer(optional)operator: "AND" | "OR"
Matching Semantics
- Location match: If
rule.targetLocationsis present and non-empty, an ad matches the location constraint ifrule.targetLocations ⊆ ad.targetLocations(i.e., the ad targets all locations required by the rule). - Age match:
- If
rule.minAgeis present, requiread.age >= rule.minAge. - If
rule.maxAgeis present, requiread.age <= rule.maxAge.
- If
- Combine the (present) constraints using
rule.operator:AND: Ad must satisfy all present constraints.OR: Ad must satisfy at least one present constraint.
- If a constraint is absent, ignore it.
Input / Output
- Input:
List<Ad> ads,Rule rule - Output:
List<String>of matchingad.idvalues, preserving the original input order.
Example
Rule: minAge = 21, targetLocations = {"US"}, operator = AND
Ads:
A:age=21,targetLocations={"US","CA"}→ matchB:age=13,targetLocations={"US"}→ no (age)C:age=25,targetLocations={"CA"}→ no (location)
Output: ["A"]
Constraints
ads.lengthcan be large; keep it efficient.- Location strings may vary in case; clarify/handle consistently (e.g., normalize to upper-case) if needed.