KatInterview

Weekly Power-User Share per Country from Impression Logs

Arrays & HashingAuto-graded · 16 test cases
MediumAuto-gradedArrays & Hashinghash-mapgroupingsets

Last updated

Group impression records by country and user over a 7-day window, flag users with 4+ active dates and 3+ surfaces on one date, and report per-country percentages.

Weekly Power-User Share per Country

You are given a list of impression records. Each record is a dictionary with these keys:

KeyMeaning
impression_idIdentifier of the impression.
user_idThe user who received the impression. May be None.
pin_idThe pin shown.
surfaceWhere the impression was shown, for example "home", "search" or "related".
impression_tsTimestamp string such as "2025-03-01 10:00:00".
country_codeCountry recorded for the impression. May be None.

You are also given today, an ISO date string such as "2025-03-04".

Window

The window is the 7 calendar dates made up of today and the 6 dates before it, both ends inclusive. The date of an impression is the date part of impression_ts, which is its first 10 characters (YYYY-MM-DD). Ignore the time of day. Impressions whose date falls outside the window are ignored.

Definitions

Treat each (country_code, user_id) pair as its own entity. A user with impressions in two countries is evaluated separately in each one, using only the impressions recorded in that country.

  • A pair is active if it has at least one impression in the window.
  • A pair is highly active if both of these hold, using only impressions in the window:
    1. It has impressions on at least 4 distinct dates.
    2. On at least one single date, it has impressions on at least 3 distinct surfaces. Surfaces seen on different dates must not be combined.

Records where user_id is None or country_code is None are skipped entirely.

Task

Implement a function that returns one dictionary per country that has at least one active user. Each dictionary has these keys:

  • country_code: the country.
  • active_users: the number of active users in that country.
  • highly_active_users: how many of those users are highly active.
  • highly_active_pct: highly_active_users / active_users * 100, rounded to two decimal places with halves rounded up (away from zero), returned as a float.

A country is included even if none of its active users are highly active (its percentage is then 0.0).

Sort the result by highly_active_pct descending, then by country_code ascending.

If no record falls in the window, return an empty list.

Constraints

  • rows may be empty.
  • impression_ts always begins with a valid YYYY-MM-DD date.
  • today is a valid ISO date string.
  • Compute the percentage with exact decimal arithmetic so that rounding is correct (avoid binary-float artifacts).
  • Duplicate impressions on the same date, or repeated surfaces on the same date, count only once toward the distinct counts.

Examples

Example 1

Input: power_user_share_by_country([{'impression_id': 1, 'user_id': 10, 'pin_id': 1, 'surface': 'home', 'impression_ts': '2025-03-01 10:00:00', 'country_code': 'US'}, {'impression_id': 2, 'user_id': 10, 'pin_id': 2, 'surface': 'search', 'impression_ts': '2025-03-01 11:00:00', 'country_code': 'US'}, {'impression_id': 3, 'user_id': 10, 'pin_id': 3, 'surface': 'related', 'impression_ts': '2025-03-01 12:00:00', 'country_code': 'US'}, {'impression_id': 4, 'user_id': 10, 'pin_id': 4, 'surface': 'home', 'impression_ts': '2025-03-02 10:00:00', 'country_code': 'US'}, {'impression_id': 5, 'user_id': 10, 'pin_id': 5, 'surface': 'home', 'impression_ts': '2025-03-03 10:00:00', 'country_code': 'US'}, {'impression_id': 6, 'user_id': 10, 'pin_id': 6, 'surface': 'home', 'impression_ts': '2025-03-04 10:00:00', 'country_code': 'US'}, {'impression_id': 7, 'user_id': 11, 'pin_id': 7, 'surface': 'home', 'impression_ts': '2025-03-04 10:00:00', 'country_code': 'US'}], '2025-03-04')

Output: [{'country_code': 'US', 'active_users': 2, 'highly_active_users': 1, 'highly_active_pct': 50}]

Example 2

Input: power_user_share_by_country([], '2025-03-04')

Output: []

Hints

Hint 1

Build two lookups while scanning the rows: the set of active dates for each (country, user), and the set of surfaces for each (country, user, date).

Hint 2

A user meets the surface rule if any one of their dates has a surface set of size 3 or more. Check this per date, never across dates.

Hint 3

Round the percentage with Decimal and ROUND_HALF_UP rather than Python's float round. Then sort with the key (-pct, country_code).