KatInterview

Survey Click-Through and Score Averages from Impression Logs

Arrays & HashingAuto-graded · 17 test cases
MediumAuto-gradedArrays & Hashinghash-mapgroupingsorting

Last updated

Group survey impression records to compute a click rate, a plain score average, and an average of each user's earliest score per survey, with careful handling of missing values.

Survey Feedback Summary

A travel app shows surveys to users. Every time a survey is displayed, one record is logged. If the user engages with it, the record also carries a click time and possibly a rating.

You are given records, a list of dictionaries, one per displayed survey. Each dictionary has these keys:

  • user_id: integer identifying the user.
  • survey_id: integer identifying the survey.
  • impression_ts: string in YYYY-MM-DD HH:MM:SS format, when the survey was shown.
  • click_ts: string in the same format, or None if the user did not click.
  • score: integer rating, or None if no rating was given.

Because the timestamps are zero-padded ISO-style strings, comparing them as strings gives chronological order.

Return a dictionary with exactly three keys:

  1. "response_rate": the number of records whose click_ts is not None, divided by the total number of records.
  2. "average_score_all": the mean of every score that is not None.
  3. "average_first_score": the mean of one score per (user_id, survey_id) pair, as defined below.

Choosing the first score of a pair

Consider only the records of a pair that have a non-None score. The pair's first score is the score of the record that comes earliest under this ordering:

  1. Smaller click_ts first, where a missing click_ts (None) counts as smaller than any real timestamp.
  2. If still tied, smaller impression_ts first.
  3. If still tied, the record that appears earlier in records.

Pairs with no scored record contribute nothing. average_first_score is the mean of the selected scores, one per pair that has at least one score.

Empty cases

  • If records is empty, response_rate is None.
  • If there are no non-None scores at all, both average_score_all and average_first_score are None.

Averages are ordinary floating-point means, not floored.

Constraints

  • 0 <= len(records) <= 10^5
  • user_id and survey_id are always present and are integers.
  • score, when present, is an integer.
  • impression_ts is always present; click_ts and score may be None.
  • Aim for roughly O(n log n) time or better.

Examples

Example 1

Input: summarize_survey_feedback([{'user_id': 101, 'survey_id': 555, 'impression_ts': '2023-09-01 10:00:00', 'click_ts': '2023-09-01 10:05:12', 'score': 4}, {'user_id': 102, 'survey_id': 555, 'impression_ts': '2023-09-01 10:02:34', 'click_ts': None, 'score': None}, {'user_id': 101, 'survey_id': 556, 'impression_ts': '2023-09-02 09:15:20', 'click_ts': '2023-09-02 09:16:00', 'score': 5}, {'user_id': 103, 'survey_id': 555, 'impression_ts': '2023-09-01 10:10:00', 'click_ts': None, 'score': None}, {'user_id': 104, 'survey_id': 557, 'impression_ts': '2023-09-03 11:00:00', 'click_ts': '2023-09-03 11:02:45', 'score': 3}])

Output: {'response_rate': 0.6, 'average_score_all': 4, 'average_first_score': 4}

Example 2

Input: summarize_survey_feedback([{'user_id': 1, 'survey_id': 10, 'impression_ts': '2024-01-01 09:00:00', 'click_ts': '2024-01-01 09:01:00', 'score': 5}, {'user_id': 1, 'survey_id': 10, 'impression_ts': '2024-01-02 09:00:00', 'click_ts': '2024-01-02 09:01:00', 'score': 2}, {'user_id': 2, 'survey_id': 10, 'impression_ts': '2024-01-01 10:00:00', 'click_ts': '2024-01-01 10:02:00', 'score': 4}])

Output: {'response_rate': 1, 'average_score_all': 3.6666666666666665, 'average_first_score': 4.5}

Example 3

Input: summarize_survey_feedback([{'user_id': 7, 'survey_id': 20, 'impression_ts': '2024-02-01 12:00:00', 'click_ts': None, 'score': None}])

Output: {'response_rate': 0, 'average_score_all': None, 'average_first_score': None}

Example 4

Input: summarize_survey_feedback([{'user_id': 3, 'survey_id': 30, 'impression_ts': '2024-03-01 08:00:00', 'click_ts': '2024-03-01 08:01:00', 'score': 1}, {'user_id': 3, 'survey_id': 31, 'impression_ts': '2024-03-01 08:02:00', 'click_ts': '2024-03-01 08:03:00', 'score': 5}])

Output: {'response_rate': 1, 'average_score_all': 3, 'average_first_score': 3}

Hints

Hint 1

Each of the three numbers has its own denominator: all records for the rate, all non-missing scores for the first average, and scored pairs for the second.

Hint 2

Pairs are keyed by (user_id, survey_id). Only scored records compete to be the pair's first score, so skip unscored ones before picking.

Hint 3

Sort the records with a key of (user_id, survey_id, click_ts or '', impression_ts or '', original index), then keep the first scored record you see for each pair. An empty string sorts before any timestamp.