The missing fields part is where I almost tripped up.
Start by clarifying the matching semantics: treat missing rule fields as wildcards (always match) and define how AND/OR compose per-field checks. Then propose an efficient data structure, such as inverted indexes on each attribute, to quickly retrieve candidate ads for repeated lookups against a static catalog.
Pro tip: Mention that for a static catalog, you can precompute and cache the inverted indexes, and for AND queries, intersect the smallest result sets first to minimize work. Also note that missing fields should be treated as 'match all' to avoid excluding ads unintentionally.
Confirm that missing rule fields mean 'no constraint' (wildcard) and that AND requires all present fields to match, while OR requires at least one present field to match. If all fields are missing, decide whether to return all ads or none based on business rules.
For each rule field, write a predicate that returns true if the field is missing or if the ad's attribute satisfies the condition. For range fields like age, check if the ad's range overlaps the rule's range; for location, check set intersection.
Combine the per-field predicates using logical AND or OR. For AND, all predicates must be true; for OR, at least one must be true. Handle the edge case where no fields are present (return all or none).
Build inverted indexes mapping each attribute value (or range bucket) to a list of ad IDs. For AND queries, intersect the smallest lists first; for OR, union the lists. Use bitsets for fast set operations if the catalog is large.
Compare linear scan vs. indexed lookup: indexing speeds up repeated queries but uses memory and build time. For static catalogs, precompute indexes; for dynamic, consider incremental updates or caching.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.