We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
I agree it is very hard, each day you need to count the unique hackers who made atleast one submission each day till that day. For example a hacker on day 5 will be counted if he has been submitted at least one submission, each day of 1,2,3,4 and 5.
Its a hard and ambigious problem.
it took alot of time.
Solution in MS SQL version
SELECTQ2.submission_date,Q3.unique_count,Q2.hacker_id,H.nameFROM/**Using sliding window to take the top hacker based on the orders given in each sub group**/(SELECTsubmission_date,submission_count,hacker_id,RANK()OVER(PARTITIONBYsubmission_dateORDERBYsubmission_countDESC,hacker_idASC)RankFROM(SELECTsubmission_date,COUNT(submission_date)assubmission_count,hacker_idFROMSubmissionsGROUPBYsubmission_date,hacker_id)Q1)Q2JOIN(#usingslidingwindowandday(date)tocheckwetherornotthesubmissionsofthehackeristhesamenumberofthedaytillthatdaySELECTsubmission_date,COUNT(DISTINCThacker_id)unique_countFROM(SELECTDISTINCT(T0.hacker_id),T0.submission_date,COUNT(T0.submission_date)OVER(PARTITIONBYT0.hacker_idORDERBYT0.submission_dateASC)subdate_countFROM/**since we only need 1 submission in each day to count the hacker,I group by the hacker to find distinct hackers in each day. If We take the whole submissions table without the group by we will miscalculate unique hackers, because we are counting hackers submission till that specific day so I will count hacker X in day 5 if that hacker have 5 submissions or more in day 5, but the problem here is we miss the fact that maybe hacker X made all 5 submissions on the day 5, so it is needed to just take one submission per day for each hacker using a simple group by function **/(SELECTsubmission_date,hacker_idFROMSubmissionsGROUPBYsubmission_date,hacker_id)T0)T1WHERET1.subdate_count>=DAY(T1.submission_date)GROUPBYsubmission_date)Q3ONQ3.submission_date=Q2.submission_dateJOINHackersHONH.hacker_id=Q2.hacker_idWHEREQ2.Rank=1
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
15 Days of Learning SQL
You are viewing a single comment's thread. Return to all comments →
I agree it is very hard, each day you need to count the unique hackers who made atleast one submission each day till that day. For example a hacker on day 5 will be counted if he has been submitted at least one submission, each day of 1,2,3,4 and 5. Its a hard and ambigious problem. it took alot of time.
Solution in MS SQL version