Untitled

mail@pastecode.io avatar
unknown
plain_text
5 months ago
2.0 kB
2
Indexable
// == Question ==
// Imagine system keeping track of customer visiting various pages on their website `somethingfun.com`
// System creates a log file for each day and each line having something like "customerId, pageId, timestamp" for
// each visit of the customer.

// Given 2 logs files log1(day-x) and log2(day-y) find loyal customers which show following properties
// - should have visited visited 2 unique pages
// - should have visited the website on each day

let day1 = "user1,page1,timestamp1\nuser1,page2,timestamp1\nuser1,page3,timestamp1\nuser2,page1,timestamp1"
let day2 = "user2,page1,timestamp1\nuser4,page2,timestamp1\nuser5,page3,timestamp1\nuser2,page1,timestamp1"
//user1, page1, timestamp1"



    function getCustomerPageVisit(dayLogFile){
      let userVisitedPageMap = new Map();
      dayLogFile.split("\n").forEach(record=>{
           record = record.split(",")
           //if user already present in that day data
           if(userVisitedPageMap.has(record[0])){
               let newRecord = userVisitedPageMap.get(record[0])
               userVisitedPageMap.set(record[0],[...newRecord,record[1]])
           }else{
               userVisitedPageMap.set(record[0],[record[1]])
           }
   
       })
       return userVisitedPageMap
    }
   


 const firstDayData =getCustomerPageVisit(day1)
 console.log("First Day Data ",firstDayData)
 const secondDayDat =getCustomerPageVisit(day2)
 console.log("secondDayDat Day Data ",secondDayDat)
 
 getLoyalCustomer(firstDayData,secondDayDat)

 let loyalUser = [];
 function getLoyalCustomer(firstDayData,secondDayDat){
    firstDayData.forEach((value,key)=>{
        console.log({key,value})
        
        if(secondDayDat.has(key)){
            let  secondDayPagevisit = secondDayDat.get(key)
            console.log("secondDayPagevisit",secondDayPagevisit)
            secondDayPagevisit.forEach(page=>{
                if(value.indexOf(page) ==-1){
                    
                }
            })

            


        }
    })
 }






Leave a Comment