Filter ads by a single rule
Company: Disney
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Onsite
Quick Answer: This question evaluates understanding of rule-based filtering for a single-rule ad filter, including set containment for locations, numeric range checks for age, handling of optional constraints, and operator logic (AND/OR).
Constraints
- 0 <= number of ads (can be large; keep the per-ad work near O(locations)).
- Location strings are compared case-insensitively (normalize to upper-case).
- ruleMinAge / ruleMaxAge use -1 as the sentinel for 'constraint absent'; real ages are >= 0.
- operator is exactly 'AND' or 'OR'.
- Output preserves the original input order of the ads.
Examples
Input: (['A', 'B', 'C'], [['US', 'CA'], ['US'], ['CA']], [21, 13, 25], ['US'], 21, -1, 'AND')
Expected Output: ['A']
Explanation: AND of {location US ⊆ ad, age >= 21}. A: US⊆{US,CA} and 21>=21 -> match. B: location ok but 13<21 -> fail. C: 25>=21 but US⊄{CA} -> fail.
Input: (['A', 'B', 'C'], [['US', 'CA'], ['US'], ['CA']], [21, 13, 25], ['US'], 21, -1, 'OR')
Expected Output: ['A', 'B', 'C']
Explanation: OR of the same two constraints. A passes both. B passes location. C passes the age constraint (25>=21). All three match.
Input: (['X', 'Y'], [['ca', 'wa'], ['CA']], [30, 30], ['CA', 'WA'], -1, -1, 'AND')
Expected Output: ['X']
Explanation: Only a location constraint is present. Case-insensitive: X targets {CA,WA} which is a superset of {CA,WA} -> match. Y targets only {CA}, missing WA -> fail.
Input: (['P', 'Q', 'R'], [['US'], ['US'], ['US']], [18, 40, 65], [], 21, 60, 'AND')
Expected Output: ['Q']
Explanation: Two age constraints (>=21 AND <=60), no location constraint. P: 18<21 fail. Q: 21<=40<=60 -> match. R: 65>60 fail.
Input: (['P', 'Q', 'R'], [['US'], ['US'], ['US']], [18, 40, 65], [], 21, 60, 'OR')
Expected Output: ['P', 'Q', 'R']
Explanation: OR of (>=21) and (<=60). P: 18<=60 ok. Q: both ok. R: 65>=21 ok. All match because each satisfies at least one bound.
Input: ([], [], [], ['US'], 21, -1, 'AND')
Expected Output: []
Explanation: Edge case: no ads, so the result is empty regardless of the rule.
Input: (['Z'], [['NY']], [50], [], -1, -1, 'AND')
Expected Output: ['Z']
Explanation: Edge case: no constraint is present (empty locations, both ages = -1), so every ad matches.
Input: (['M', 'N'], [['TX'], ['TX']], [10, 99], ['TX'], 18, 80, 'OR')
Expected Output: ['M', 'N']
Explanation: OR of {TX⊆ad, age>=18, age<=80}. M: TX location matches -> match (despite age 10). N: TX location matches -> match. Both pass via the location clause.
Hints
- Decide which constraints are PRESENT first (non-empty ruleLocations, ruleMinAge >= 0, ruleMaxAge >= 0). Only present constraints participate in the AND/OR combination.
- Location match means the rule's locations are a SUBSET of the ad's locations (rule ⊆ ad), not equality and not intersection. Upper-case both sides before comparing.
- If no constraint is present, every ad matches. For 'AND' use all-of, for 'OR' use any-of over just the present constraints.