KK's blog

每天积累多一些

0%

Karat 004 Ads Conversion

A list of user ids + IPs, a list of user ids who have made purchases, a list of advertisement
clicks with user IPs.
Each user id has at most 1 IP.

Output: for each ad, output the number of clicks and the number of purchases.

completed_purchase_user_ids = ["123"]

ad_clicks = [
    #"IP_Address,Time,Ad_Text",
    "127.0.0.1,2011-01-03 09:21:22,black pen"]

all_user_ips = [
    #"User_ID,IP_Address",
        "123,127.0.0.1"]

输出:
black pen, 1 click, 1 purchase

题目大意:

给定购买记录,click记录,ip地址。求每个产品点击数和购买次数

解题思路:

此题比较直观,点击数直接可以从click记录中获得,购买次数就是将三个表格join一起获得

解题步骤:

N/A

注意事项:

  1. Python代码:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    def get_ad_clicks_purchases(self, completed_purchase_user_ids, ad_clicks, all_user_ips):
    # get clicks
    product_to_clicks = collections.defaultdict(int)
    for ad_click in ad_clicks:
    parts = ad_click.split(',')
    product_to_clicks[parts[2]] += 1

    # get purchases
    userid_to_ip = collections.defaultdict(str)
    for all_user_ip in all_user_ips:
    parts = all_user_ip.split(',')
    userid_to_ip[parts[0]] = parts[1]

    ip_to_product = collections.defaultdict(str)
    for ad_click in ad_clicks:
    parts = ad_click.split(',')
    ip_to_product[parts[0]] = parts[2]

    product_to_purchase = collections.defaultdict(int)
    for user_id in completed_purchase_user_ids:
    ip = userid_to_ip[user_id]
    product = ip_to_product[ip]
    product_to_purchase[product] += 1

    res = []
    for product, click in product_to_clicks.items():
    res.append((product, click, product_to_purchase[product]))
    return res

算法分析:

时间复杂度为O(n + m + p),空间复杂度O(n + m + p), n, m, p分别每个表的大小

Free mock interview