Coding problem
The three most visited pages
The task
The site's dashboard shows its three busiest pages.
Complete TopThreePages(List<Visit> visits). Visit is a record with the Page that was visited and the Visitor. Count the visits to each page: every visit counts, including repeat visits by the same visitor. Return the three pages with the most visits as PageVisits, the most visits first; pages with the same number of visits are sorted by Page in ascending ordinal order, which compares characters by their numeric values. Fewer than three pages gives all of them. Pages match exactly, letter case included. An empty list gives an empty list. The list and its visits are never null.
Signature
public record Visit(string Page, string Visitor); public record PageVisits(string Page, int Visits); public static List<PageVisits> TopThreePages(List<Visit> visits)
Examples
[("/home", "amy"), ("/shop", "ben"), ("/home", "ben"), ("/blog", "amy"), ("/shop", "amy"), ("/home", "cara"), ("/help", "dev")]→[("/home", 3), ("/shop", 2), ("/blog", 1)]. /blog and /help tie on one visit, and /blog comes first by name.[("/home", "amy"), ("/home", "amy")]→[("/home", 2)]. Every visit counts, even a second one by the same visitor.
Limits: Up to 500,000 visits to up to 100,000 pages.
The tests check: an empty list, repeat visits, fewer than three pages, the count deciding before the name, a tie for third settled by page name, letter case in page names, and 500,000 visits in time.
Hints
Your code
Already included: using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net.Http; using System.Threading; using System.Threading.Tasks;
public record Visit(string Page, string Visitor);
public record PageVisits(string Page, int Visits);
public static class Solution
{
// The three pages with the most visits, most first, ties by page name.
public static List<PageVisits> TopThreePages(List<Visit> visits)
{
throw new NotImplementedException();
}
}This problem comes with TechInterviewNotes Full Access
Unlock all paid content on TechInterviewNotes.