Two sets, one for topics one for companies, check both before accepting and add to both after.
Use two hash sets to track seen topics and seen companies, iterating through the stream once and accepting an ad only if both its topic and company are new. This achieves O(N) time and O(N) space. Be prepared to discuss edge cases like empty stream, null values, and duplicate ads.
Pro tip: Clarify whether the stream is infinite and if memory is a concern; if so, propose a probabilistic data structure like Bloom filter for space efficiency, but note the trade-off in accuracy.
Restate the filtering condition: an ad is accepted only if its topic and company have not appeared in any previously accepted ad. Confirm that the stream is processed in order and that we need to output the filtered stream.
Use two hash sets: one for seen topics and one for seen companies. Hash sets provide O(1) average-time lookups and insertions, ensuring overall O(N) time.
Iterate through each ad in the stream. For each ad, check if its topic is in the seen topics set or its company is in the seen companies set. If neither is present, accept the ad and add its topic and company to the respective sets.
Time complexity is O(N) because each ad is processed once with O(1) set operations. Space complexity is O(N) in the worst case, as we may store up to N topics and N companies.
Cover edge cases: empty stream, stream with all ads having unique topics and companies, stream with duplicates, ads with null or missing fields, and large streams to verify performance.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.