DisneyCodingFree preview
Single-Rule Ad Filter
Phone-screen problem: given List<Ad> (id, targetLocations set, age) and a Rule (optional targetLocations, optional minAge / maxAge, operator AND | OR), return matching ad ids in input order. Key work is defining how optional criteria combine under AND vs OR, plus normalizing location casing.
SWE
ads
set
object-design
Frequency
Single report
Last asked
2026-01-12
Stage
phone-screen
Problem
When a user watches content, the system filters an ad pool to find ads that match a single targeting rule. Implement a lightweight filter for one rule with an operator.
Data model
Adid: StringtargetLocations: Set<String>(e.g.["CA", "TX"])age: int(intended viewer age for the ad)
RuletargetLocations: Set<String>(optional, e.g.["ca", "wa"])minAge: Integer(optional)maxAge: Integer(optional)operator: AND | OR
Input / output
- Input:
List<Ad> ads,Rule rule - Output:
List<String>of matchingad.ids, in the same order as the input list. - Location criterion: the ad's location set must overlap the rule's set (set intersection). Age criterion:
minAge/maxAgebound the ad'sage.
Examples
Rule: minage >= 21, targetLocations = {"US"}, operator = AND
Ads:
A: minage 21, targetLocations {US, CA} -> match
B: minage 13, targetLocations {US} -> age fails
C: minage 25, targetLocations {CA} -> location fails
Output: ["A"]Hints
Pitfalls
- Location casing is inconsistent in the prompt itself. Ad locations are written uppercase (
"CA","TX") while the rule example uses lowercase ("ca","wa"). Ask whether matching is case-insensitive before writing the comparison. - Optional criteria. Any of
targetLocations,minAge,maxAgecan be absent. Decide (and confirm) whether an absent criterion is skipped entirely, and what that means underOR(an absent criterion must not make every ad match by default). - Field naming drift. The data model calls the ad field
age, while the example labels itminage. Treat it as the singleagevalue and state the assumption out loud. - Location semantics. The example treats the location criterion as "the ad's location set intersects the rule's set" (
{US, CA}matches{US}). Confirm intersection versus subset. - Bound inclusivity. Whether
minAge/maxAgeare inclusive is unspecified; state the assumption before coding. - Order preservation. Output must follow input order; don't collect into an unordered set.
- Set built inside the loop. Rebuilding the target set per ad silently adds
O(k)work per ad; build it once before the loop.
Talking Points
- Declare the inclusive-bounds assumption first. The prompt is silent, and flagging the ambiguity before coding shows you catch it.
- Build the target set once, outside the loop. Otherwise every ad pays an extra
O(k). - Use set intersection instead of hand-iterating targets. It is idiomatic, concise, and runs in C-level set operations.
- Compute the booleans first, then combine by operator. The AND/OR branch becomes two lines that are easy to verify.
- Order is preserved by construction. Appending during a forward pass needs no sort or index map.
- Mention the tradeoff. With a huge ad list and a tiny target list, a plain per-target check may beat set overhead, but the set approach is the right default.
Follow-ups
Preparation
- Write the filter in Java with an explicit
matchesLocation,matchesAge, and combiner step, so each criterion can be marked "not applicable" when its field is null. - Run the example by hand, then add cases for: no criteria set, only
maxAge,ORwith one failing criterion, and mixed-case locations.
Was this article helpful?
Comments
Sign in to join the discussion
Loading...
