{"status":200,"message":"","question":{"questionId":6,"heading":"Design a Hit Counter/Webpage Visits Counter - Multi-Threaded","urlSuffix":"design-hit-counter-multithreaded","descriptionS3Uri":"s3://codepractice1/questions/6/q06_webpage_visit_counter.html","type":0,"difficulty":1,"thumbnailUrl":"https://public-items-1.s3.ap-south-1.amazonaws.com/codezym/seo_images/q06_sddefault.jpg","designPatterns":"","languages":"java,python","isPrivate":false,"companies":[{"name":"Amazon LLD","code":"amazon"},{"name":"Adobe","code":"adobe"},{"name":"Atlassian","code":"atlassian"},{"name":"Salesforce LLD","code":"salesforce"},{"name":"Visa","code":"visa"},{"name":"Microsoft LLD","code":"microsoft"},{"name":"Apple","code":"apple"},{"name":"Uber LLD","code":"uber"},{"name":"Oracle","code":"oracle"}],"questionCategory":0,"aiMockQuestionId":-1,"tags":[]},"description":"\r\nWrite code for low level design of a webpage visits counter.  <br />\r\nThere are n webpages in a website numbered 0 to n-1. <br />\r\nHundreds of users visit webpages of this website simultaneously. <br />\r\nYou have to record visit count for each page and return them when required.\r\n<br /> <br />\r\n\r\n<strong>Note : </strong> <br />\r\n- For <strong>Java</strong>, code will be tested in a MULTI-THREADED environment, so use thread safe data structures and handle synchronization properly. <br />\r\n-  For <strong>Python</strong>,  code will be tested in a single threaded environment   <br />\r\n- There will be at max 1000 webpages\r\n\r\n<br /> <br />\r\n\r\nYour solution should implement below methods : <br /> <br />\r\n\r\n<strong>Method :  init(int totalPages, Helper06 helper)  </strong> <br />\r\n- totalPages is the 'n' we discussed above i.e. total number of webpages in the website.  <br />\r\n- Use this method to initialize your instance variables <br />\r\n- use helper's methods for printing logs else logs will not be visible.\r\n<br /> <br />\r\n\r\n<strong>Method : incrementVisitCount(int pageIndex) </strong> <br />\r\n- increment visit count for webpage at pageIndex by 1.\r\n<br /> <br />\r\n\r\n<strong>Method : getVisitCount(int pageIndex) </strong> <br />\r\n- return total visit count for a given page\r\n<br /> <br />\r\n For the same pageIndex, <i>incrementVisitCount()</i> and\r\n <i>getVisitCount()</i> <strong>will never be called concurrently. </strong> <br>\r\n This is done to maintain correctness and eventual consistency in the system.\r\n<br /> <br />\r\n<strong>Example : </strong> <br />\r\n<span class =  'text-blue-700' >init</span>(totalPages = 2, helper = helper)  <br /> <br />\r\n<span class =  'text-blue-700' >incrementVisitCount</span>(pageIndex = 0)  <br />\r\n<span class =  'text-blue-700' >incrementVisitCount</span>(pageIndex = 1)  <br />\r\n<span class =  'text-blue-700' >incrementVisitCount</span>(pageIndex = 1)  <br />\r\n<span class =  'text-blue-700' >incrementVisitCount</span>(pageIndex = 1)  <br />\r\n<span class =  'text-blue-700' >incrementVisitCount</span>(pageIndex = 0)  <br />\r\n<br />\r\n<span class =  'text-blue-700' >getVisitCount</span>(pageIndex = 0) : returns <span class =  'text-blue-700' >2</span>  <br />\r\n<span class =  'text-blue-700' >getVisitCount</span>(pageIndex = 1) : returns <span class =  'text-blue-700' >3</span>  <br />\r\n\r\n"}