PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep

Quick Overview

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).

  • medium
  • Disney
  • Coding & Algorithms
  • Software Engineer

Filter ads by a single rule

Company: Disney

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Onsite

## Single-Rule Ad Filter ### Context When a user watches content, the system needs to filter an ad pool to find ads that match **one rule**. Implement a lightweight filter that supports a **single rule** with an **operator**. ### Data model **Ad** - `id: String` - `targetLocations: 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.targetLocations` is present and non-empty, then an ad matches the location constraint if `rule.targetLocations ⊆ ad.targetLocations` (i.e., the ad targets *all* locations required by the rule). - **Age match**: - if `rule.minAge` is present, require `ad.age >= rule.minAge` - if `rule.maxAge` is present, require `ad.age <= rule.maxAge` - 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 matching `ad.id` values, **preserving the original input order** ### Example Rule: `minAge = 21`, `targetLocations = {"US"}`, `operator = AND` Ads: - `A`: `age=21`, `targetLocations={"US","CA"}` → match - `B`: `age=13`, `targetLocations={"US"}` → no (age) - `C`: `age=25`, `targetLocations={"CA"}` → no (location) Output: `["A"]` ### Constraints (assume) - `ads.length` can be large; keep it efficient. - Location strings may vary in case; clarify/handle consistently (e.g., normalize to upper-case) if needed.

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).

When a user watches content, the system filters an ad pool to find ads matching ONE rule with an operator. Each ad is described by three parallel arrays: - `adIds[i]` — the string id of ad i - `adLocations[i]` — the list of locations ad i targets (e.g. ["US", "CA"]) - `adAges[i]` — the intended viewer age (int) for ad i The rule is given as: - `ruleLocations` — list of required locations; if empty, the location constraint is absent - `ruleMinAge` — int; `-1` means absent - `ruleMaxAge` — int; `-1` means absent - `operator` — either `"AND"` or `"OR"` **Matching semantics** - Location constraint (only when `ruleLocations` is non-empty): the ad matches if every required location is contained in the ad's locations — i.e. `ruleLocations ⊆ adLocations`. Location strings are compared case-insensitively (normalize to upper-case). - Age min constraint (only when `ruleMinAge >= 0`): `adAge >= ruleMinAge`. - Age max constraint (only when `ruleMaxAge >= 0`): `adAge <= ruleMaxAge`. - Combine the **present** constraints with `operator`: - `AND` → the ad must satisfy ALL present constraints. - `OR` → the ad must satisfy AT LEAST ONE present constraint. - An absent constraint is ignored. If NO constraint is present, every ad matches. **Output**: the list of matching `adIds`, preserving the original input order. **Example** Rule: `ruleMinAge = 21`, `ruleLocations = ["US"]`, `operator = AND` Ads: A (age 21, {US,CA}) → match; B (age 13, {US}) → fails age; C (age 25, {CA}) → fails location. Output: `["A"]`.

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

  1. Decide which constraints are PRESENT first (non-empty ruleLocations, ruleMinAge >= 0, ruleMaxAge >= 0). Only present constraints participate in the AND/OR combination.
  2. 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.
  3. If no constraint is present, every ad matches. For 'AND' use all-of, for 'OR' use any-of over just the present constraints.
Last updated: Jun 26, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,000+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Generate All Valid Combinations of Balanced Parentheses - Disney (medium)
  • Generate All Well-Formed Parenthesis Strings - Disney (medium)
  • Implement an LRU cache - Disney (medium)
  • Determine if chasing points will meet - Disney (medium)
  • Solve matrix add, frequency count, longest consecutive - Disney (Medium)