KatInterview

Rank Advertisers by Reports, Then Report Rate

Heaps, Sorting & SearchAuto-graded · 17 test cases
EasyAuto-gradedHeaps, Sorting & Searchhashingsortingaggregation

Last updated

Aggregate per-ad records by advertiser, then rank with a multi-key sort (total reports, report rate, id) and return the top k.

A trust-and-safety team keeps one record per advertisement. Each record says which advertiser owns the ad, how many people visited the ad's page, and how many of those people flagged the ad as harmful. You need to find the advertisers who look the worst.

Task

Implement a function that takes a list of ad records and an integer k, and returns the ids of the k worst advertisers, worst first.

Each record is a dictionary with these keys:

  • "advertiser_id": a string identifying the advertiser.
  • "page_visits": a non-negative integer, the visits to this ad's page.
  • "bad_reports": a non-negative integer, the number of reports filed against this ad.

An advertiser may own many ads, so the same advertiser_id can appear in several records.

Scoring rules

  1. Combine per advertiser. For each advertiser, add up bad_reports over all of their records to get total_reports. Do the same for page_visits to get total_visits.
  2. Report rate. report_rate = total_reports / total_visits. If total_visits is 0, the rate is defined as 0.
  3. Ordering. Rank advertisers by these keys in order:
    1. Larger total_reports first.
    2. If tied, larger report_rate first.
    3. If still tied, advertiser_id in ascending (lexicographic) order.

Compute the rate with ordinary true (floating-point) division and compare the resulting values.

Output

Return a list of the first k advertiser ids in this order.

  • If k is larger than the number of distinct advertisers, return all of them.
  • If k <= 0, return an empty list.
  • Advertisers with zero reports are still ranked, so they appear after everyone with more reports.

Constraints

  • 0 <= len(records) <= 10^5
  • page_visits and bad_reports are integers >= 0
  • k is an integer and may be zero, negative, or larger than the number of advertisers
  • Each record contains all three keys
  • Advertiser ids are non-empty strings

Examples

Example 1

Input: rank_flagged_advertisers([{'advertiser_id': 'a1', 'page_visits': 1000, 'bad_reports': 80}, {'advertiser_id': 'a2', 'page_visits': 100, 'bad_reports': 30}, {'advertiser_id': 'a1', 'page_visits': 500, 'bad_reports': 20}, {'advertiser_id': 'a3', 'page_visits': 50, 'bad_reports': 30}], 2)

Output: ['a1', 'a3']

Example 2

Input: rank_flagged_advertisers([{'advertiser_id': 'shop_x', 'page_visits': 10, 'bad_reports': 0}, {'advertiser_id': 'shop_y', 'page_visits': 10, 'bad_reports': 2}, {'advertiser_id': 'shop_z', 'page_visits': 1000, 'bad_reports': 2}], 3)

Output: ['shop_y', 'shop_z', 'shop_x']

Hints

Hint 1

Group the records by advertiser first. A dictionary that holds running totals of reports and visits per advertiser is enough.

Hint 2

Compute the rate only after summing across all of an advertiser's records, and guard against a total of zero visits.

Hint 3

Sort with a key tuple. Negate the numeric fields so they sort descending while the advertiser id stays ascending, then slice off the first k.