# Ads Conversion Rate

```javascript
// The people who buy ads on our network don't have enough data about how ads are working for
//their business. They've asked us to find out which ads produce the most purchases on their website.

// Our client provided us with a list of user IDs of customers who bought something on a landing page
//after clicking one of their ads:

// # Each user completed 1 purchase.
// completed_purchase_user_ids = [
//   "3123122444","234111110", "8321125440", "99911063"]

// And our ops team provided us with some raw log data from our ad server showing every time a
//user clicked on one of our ads:
// ad_clicks = [
//  #"IP_Address,Time,Ad_Text",
//  "122.121.0.1,2016-11-03 11:41:19,Buy wool coats for your pets",
//  "96.3.199.11,2016-10-15 20:18:31,2017 Pet Mittens",
//  "122.121.0.250,2016-11-01 06:13:13,The Best Hollywood Coats",
//  "82.1.106.8,2016-11-12 23:05:14,Buy wool coats for your pets",
//  "92.130.6.144,2017-01-01 03:18:55,Buy wool coats for your pets",
//  "92.130.6.145,2017-01-01 03:18:55,2017 Pet Mittens",
//]
       
//The client also sent over the IP addresses of all their users.
       
//all_user_ips = [
//  #"User_ID,IP_Address",
//   "2339985511,122.121.0.155",
//  "234111110,122.121.0.1",
//  "3123122444,92.130.6.145",
//  "39471289472,2001:0db8:ac10:fe01:0000:0000:0000:0000",
//  "8321125440,82.1.106.8",
//  "99911063,92.130.6.144"
//]
       
// Write a function to parse this data, determine how many times each ad was clicked,
//then return the ad text, that ad's number of clicks, and how many of those ad clicks
//were from users who made a purchase.


// Expected output:
// Bought Clicked Ad Text
// 1 of 2  2017 Pet Mittens
// 0 of 1  The Best Hollywood Coats
// 3 of 3  Buy wool coats for your pets
```

### Solution:

```
 public List<String> adsConversionRate(String[] completedPurchaseUserIds, String[] adClicks, String[] allUserIps)
    {
        Map<String, int[]> resultMap = new HashMap<>();
        Map<String, String> userIpMap = new HashMap<>();
        // ip --> adtitle
        Map<String, String> ipToTitle = new HashMap<>();

        for(int i = 0; i< allUserIps.length; i++)
        {
            Stirng[] element = allUserIps[i].split(",");
            // userId --> ip
            userIpMap.put(element[0], element[1]);
        }


        for(int i = 0; i< adClicks.length; i++)
        {
            String[] clicksArray = adClicks[i].split(",");
            String adTitle = clicksArray[2];
            if(!resultMap.containsKey(adTitle))
            {
                int[] purchaseOfClick = new int[2];
                purchaseOfClick[1] = 1; 
                resultMap.put(adTitle, purchaseOfClick);
            }
            else
            {
                int[] purchaseOfClick = resultMap.get(adTitle);
                purchaseOfClick[1]++;
                resultMap.put(adTitle, purchaseOfClick);
            }
            
            // ip --> titile
            ipToTitle.put(clicksArray[0], adTitle);
        }


       for(int i = 0; i< completedPurchaseUserIds.length; i++)
       {
            String purchasedIp = userIpMap.get(completedPurchaseUserIds[i]);
            String title = ipToTitle.get(purchasedIp);
            int[] purchaseHelper = resultMap.get(title);
            purchaseHelper[0]++;
            resultMap.put(title, purchaseHelper);
       }
    }
```

\*\*

```
   public Map<String, int[]> adsConversionRate(String[] completedPurchaseUserIds, 
                                               String[] adClicks, 
                                               String[] allUserIps)
    {
        // int[0] is purchase, int[1] is click
        Map<String, int[]> resultMap = new HashMap<>();
        // userId --> ip
        Map<String, String> userIpMap = new HashMap<>();
        Set<String> useIds = new HashSet<>(completedPurchaseUserIds);

        for(int i = 0; i< allUserIps.length; i++)
        {
            Stirng[] element = allUserIps[i].split(",");
            // userId --> ip
            userIpMap.put(element[0], element[1]);
        }

        for(int i = 0; i< adClicks.length; i++)
        {
            String[] clicksArray = adClicks[i].split(",");
            String ip = clicksArray[0];
            String adTitle = clicksArray[2];
            
            if(!resultMap.containsKey(adTitle))
            {
                int[] purchaseOfClick = new int[2];
                purchaseOfClick[0] = useIds.contains(userIpMap.get(ip)) ? 1 : 0;
                purchaseOfClick[1] = 1; 
                resultMap.put(adTitle, purchaseOfClick);                
            }
            else
            {
                int[] purchaseOfClick = resultMap.get(adTitle);
                if(useIds.contains(userIpMap.get(ip)))
                {
                    purchaseOfClick[0]++;
                }
                purchaseOfClick[1]++;
                resultMap.put(adTitle, purchaseOfClick);
            }
        }
        return resultMap;
    }
```
