{"id":102,"date":"2019-06-01T12:22:22","date_gmt":"2019-06-01T12:22:22","guid":{"rendered":"https:\/\/www.369usa.com\/blog\/?p=102"},"modified":"2019-06-01T12:22:22","modified_gmt":"2019-06-01T12:22:22","slug":"tech-interview-exam-question-linked-list-random-node","status":"publish","type":"post","link":"https:\/\/www.369usa.com\/blog\/tech-interview-exam-question-linked-list-random-node\/","title":{"rendered":"Tech Interview Exam Question: Linked List Random Node"},"content":{"rendered":"<p>Given a singly linked list, return a random node&#8217;s value from the linked list. Each node must have the same probability of being chosen.<\/p>\n<p>Follow up:<br \/>\nWhat if the linked list is extremely large and its length is unknown to you? Could you solve this efficiently without using extra space?<\/p>\n<p>Example:<\/p>\n<p>\/\/ Init a singly linked list [1,2,3].<br \/>\nListNode head = new ListNode(1);<br \/>\nhead.next = new ListNode(2);<br \/>\nhead.next.next = new ListNode(3);<br \/>\nSolution solution = new Solution(head);<\/p>\n<p>\/\/ getRandom() should return either 1, 2, or 3 randomly. Each element should have equal probability of returning.<br \/>\nsolution.getRandom();<\/p>\n<pre>\r\n\r\n\/**\r\n * Definition for singly-linked list.\r\n * function ListNode(val) {\r\n *     this.val = val;\r\n *     this.next = null;\r\n * }\r\n *\/\r\n\/**\r\n * @param head The linked list's head.\r\n        Note that the head is guaranteed to be not null, so it contains at least one node.\r\n * @param {ListNode} head\r\n *\/\r\nvar Solution = function(head) {\r\n    this.head = head;\r\n};\r\n\r\n\/**\r\n * Returns a random node's value.\r\n * @return {number}\r\n *\/\r\nSolution.prototype.getRandom = function() {\r\n    \r\n    var total = 1;\r\n    var p = this.head;\r\n    while(p.next !== null){\r\n        p = p.next;\r\n        total++;\r\n    }\r\n    \r\n    var index =  Math.floor(Math.random() * Math.floor(total))\r\n    var p = this.head;\r\n    for(var i=0;i&lt;index;i++){\r\n        p = p.next;\r\n    }\r\n    \r\n    return p.val;\r\n    \r\n};\r\n\r\n\/** \r\n * Your Solution object will be instantiated and called as such:\r\n * var obj = new Solution(head)\r\n * var param_1 = obj.getRandom()\r\n *\/\r\n\r\n<\/pre>\n<p>Random Set- Phone Interview<br \/>\nCompletedJune 1, 2019 7:41 AM<br \/>\nTime Spent: 38 minutes 29 seconds<br \/>\nTime Allotted: 1 hour 30 minutes<br \/>\nIn order to provide you a more accurate assessment, we are waiting for more users to take this interview.<br \/>\nEmail me when my report is ready<br \/>\nFind Pivot Index<br \/>\nEASY<br \/>\n741 \/ 741 Testcases Passed<br \/>\nDetails<br \/>\nLinked List Random Node<br \/>\nMEDIUM<br \/>\n7 \/ 7 Testcases Passed<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Given a singly linked list, return a random node&#8217;s value from the linked list. Each node must have the same probability of being chosen. Follow up: What if the linked list is extremely large and its length is unknown to you? Could you solve this efficiently without using extra space? Example: \/\/ Init a singly &hellip; <a href=\"https:\/\/www.369usa.com\/blog\/tech-interview-exam-question-linked-list-random-node\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Tech Interview Exam Question: Linked List Random Node&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[],"class_list":["post-102","post","type-post","status-publish","format-standard","hentry","category-6"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v23.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Tech Interview Exam Question: Linked List Random Node - \u7ebd\u7ea6\u7f51\u7ad9\u8bbe\u8ba1\u535a\u5ba2<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.369usa.com\/blog\/tech-interview-exam-question-linked-list-random-node\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Tech Interview Exam Question: Linked List Random Node - \u7ebd\u7ea6\u7f51\u7ad9\u8bbe\u8ba1\u535a\u5ba2\" \/>\n<meta property=\"og:description\" content=\"Given a singly linked list, return a random node&#8217;s value from the linked list. Each node must have the same probability of being chosen. Follow up: What if the linked list is extremely large and its length is unknown to you? Could you solve this efficiently without using extra space? Example: \/\/ Init a singly &hellip; Continue reading &quot;Tech Interview Exam Question: Linked List Random Node&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.369usa.com\/blog\/tech-interview-exam-question-linked-list-random-node\/\" \/>\n<meta property=\"og:site_name\" content=\"\u7ebd\u7ea6\u7f51\u7ad9\u8bbe\u8ba1\u535a\u5ba2\" \/>\n<meta property=\"article:published_time\" content=\"2019-06-01T12:22:22+00:00\" \/>\n<meta name=\"author\" content=\"darkersss\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"darkersss\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.369usa.com\/blog\/tech-interview-exam-question-linked-list-random-node\/\",\"url\":\"https:\/\/www.369usa.com\/blog\/tech-interview-exam-question-linked-list-random-node\/\",\"name\":\"Tech Interview Exam Question: Linked List Random Node - \u7ebd\u7ea6\u7f51\u7ad9\u8bbe\u8ba1\u535a\u5ba2\",\"isPartOf\":{\"@id\":\"https:\/\/www.369usa.com\/blog\/#website\"},\"datePublished\":\"2019-06-01T12:22:22+00:00\",\"dateModified\":\"2019-06-01T12:22:22+00:00\",\"author\":{\"@id\":\"https:\/\/www.369usa.com\/blog\/#\/schema\/person\/1c260c42af4aadda7f89a0081f6591b6\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.369usa.com\/blog\/tech-interview-exam-question-linked-list-random-node\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.369usa.com\/blog\/tech-interview-exam-question-linked-list-random-node\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.369usa.com\/blog\/tech-interview-exam-question-linked-list-random-node\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.369usa.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Tech Interview Exam Question: Linked List Random Node\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.369usa.com\/blog\/#website\",\"url\":\"https:\/\/www.369usa.com\/blog\/\",\"name\":\"369\u7ebd\u7ea6\u7f51\u7ad9\u8bbe\u8ba1\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.369usa.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.369usa.com\/blog\/#\/schema\/person\/1c260c42af4aadda7f89a0081f6591b6\",\"name\":\"darkersss\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.369usa.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/2c538d84b753eed6f6f714becf9b3955?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/2c538d84b753eed6f6f714becf9b3955?s=96&d=mm&r=g\",\"caption\":\"darkersss\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Tech Interview Exam Question: Linked List Random Node - \u7ebd\u7ea6\u7f51\u7ad9\u8bbe\u8ba1\u535a\u5ba2","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.369usa.com\/blog\/tech-interview-exam-question-linked-list-random-node\/","og_locale":"en_US","og_type":"article","og_title":"Tech Interview Exam Question: Linked List Random Node - \u7ebd\u7ea6\u7f51\u7ad9\u8bbe\u8ba1\u535a\u5ba2","og_description":"Given a singly linked list, return a random node&#8217;s value from the linked list. Each node must have the same probability of being chosen. Follow up: What if the linked list is extremely large and its length is unknown to you? Could you solve this efficiently without using extra space? Example: \/\/ Init a singly &hellip; Continue reading \"Tech Interview Exam Question: Linked List Random Node\"","og_url":"https:\/\/www.369usa.com\/blog\/tech-interview-exam-question-linked-list-random-node\/","og_site_name":"\u7ebd\u7ea6\u7f51\u7ad9\u8bbe\u8ba1\u535a\u5ba2","article_published_time":"2019-06-01T12:22:22+00:00","author":"darkersss","twitter_card":"summary_large_image","twitter_misc":{"Written by":"darkersss","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.369usa.com\/blog\/tech-interview-exam-question-linked-list-random-node\/","url":"https:\/\/www.369usa.com\/blog\/tech-interview-exam-question-linked-list-random-node\/","name":"Tech Interview Exam Question: Linked List Random Node - \u7ebd\u7ea6\u7f51\u7ad9\u8bbe\u8ba1\u535a\u5ba2","isPartOf":{"@id":"https:\/\/www.369usa.com\/blog\/#website"},"datePublished":"2019-06-01T12:22:22+00:00","dateModified":"2019-06-01T12:22:22+00:00","author":{"@id":"https:\/\/www.369usa.com\/blog\/#\/schema\/person\/1c260c42af4aadda7f89a0081f6591b6"},"breadcrumb":{"@id":"https:\/\/www.369usa.com\/blog\/tech-interview-exam-question-linked-list-random-node\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.369usa.com\/blog\/tech-interview-exam-question-linked-list-random-node\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.369usa.com\/blog\/tech-interview-exam-question-linked-list-random-node\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.369usa.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Tech Interview Exam Question: Linked List Random Node"}]},{"@type":"WebSite","@id":"https:\/\/www.369usa.com\/blog\/#website","url":"https:\/\/www.369usa.com\/blog\/","name":"369\u7ebd\u7ea6\u7f51\u7ad9\u8bbe\u8ba1","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.369usa.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.369usa.com\/blog\/#\/schema\/person\/1c260c42af4aadda7f89a0081f6591b6","name":"darkersss","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.369usa.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/2c538d84b753eed6f6f714becf9b3955?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/2c538d84b753eed6f6f714becf9b3955?s=96&d=mm&r=g","caption":"darkersss"}}]}},"_links":{"self":[{"href":"https:\/\/www.369usa.com\/blog\/wp-json\/wp\/v2\/posts\/102"}],"collection":[{"href":"https:\/\/www.369usa.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.369usa.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.369usa.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.369usa.com\/blog\/wp-json\/wp\/v2\/comments?post=102"}],"version-history":[{"count":1,"href":"https:\/\/www.369usa.com\/blog\/wp-json\/wp\/v2\/posts\/102\/revisions"}],"predecessor-version":[{"id":103,"href":"https:\/\/www.369usa.com\/blog\/wp-json\/wp\/v2\/posts\/102\/revisions\/103"}],"wp:attachment":[{"href":"https:\/\/www.369usa.com\/blog\/wp-json\/wp\/v2\/media?parent=102"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.369usa.com\/blog\/wp-json\/wp\/v2\/categories?post=102"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.369usa.com\/blog\/wp-json\/wp\/v2\/tags?post=102"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}