{"id":708,"date":"2017-02-15T18:48:40","date_gmt":"2017-02-16T02:48:40","guid":{"rendered":"http:\/\/www.tech.dimprash.com\/?p=708"},"modified":"2023-02-04T12:17:05","modified_gmt":"2023-02-04T20:17:05","slug":"ten-x-interview","status":"publish","type":"post","link":"http:\/\/www.tech.dimprash.com\/?p=708","title":{"rendered":"Ten-X Interview"},"content":{"rendered":"<p>1) Write a javascript function to print an array slowly (e.g each element in intervals of 500 ms). Example of array : 1,5,6,&#8217;cool&#8217;,8. Once done then print &#8220;all done&#8221;.<\/p>\n<p><strong>Method-1<\/strong><\/p>\n<pre>var arr = [1,5,6,\"cool\",8];\r\n\r\nfunction foo(i) {\r\n   console.log(\"i:\" + i + \":\" + arr[i]);\r\n\r\n}\r\n\r\nfunction foo2() {\r\n   console.log(\"all done\");\r\n}\r\n\r\nfor (i=1; i &lt;=3 ; i++) {\r\n    setTimeout(foo, i*500, i);\r\n}\r\n\r\nsetTimeout(foo2, (i)*500);\r\n<\/pre>\n<p>Disadvantage in Method-1 is that so many setTimeout will be in memory. In Method-2 the number of setTimeout in memory will be less.<\/p>\n<p><strong>Method-2<\/strong><\/p>\n<pre>var arr = [1,5,6,'cool', 8];\r\n\r\nfunction foo(i) {\r\nif (i &lt; arr.length) {\r\nconsole.log(\"i:\" + i + \":\" + arr[i]);\r\nsetTimeout(foo, 500, i+1);\r\n} else {\r\nconsole.log(\"all done\");\r\n}\r\n}\r\n\r\nsetTimeout(foo, 500, 0);<\/pre>\n<p><strong>Method-3 <\/strong><\/p>\n<pre>const arr = [10, 12, 15, 21];\r\nfor (let i = 0; i &lt; arr.length; i++) {\r\n  \/\/ using the ES6 let syntax, it creates a new binding\r\n  \/\/ every single time the function is called\r\n  \/\/ read more here: http:\/\/exploringjs.com\/es6\/ch_variables.html#sec_let-const-loop-heads\r\n  setTimeout(function() {\r\n    console.log('The index of this number is: ' + i);\r\n  }, 3000*i);\r\n\r\n<\/pre>\n<p><strong>Method-4 <\/strong><\/p>\n<pre>for (i = 0, max = 9; i &lt; max ; i ++) {\r\n  setTimeout(printFunc(i), 500*i);\r\n}\r\n\r\nfunction printFunc(i) {\r\n   return function() {\r\n       console.log(i);\r\n   }\r\n}\r\n<\/pre>\n<p><strong>Method-5. (Using IIFE &#8211; Immediately Invoked Function Expression)<br \/>\n<\/strong><\/p>\n<pre>var arr = [1,5,6,\"cool\",8];\r\n\r\nfor (i=0; i &lt; arr.length ; i++) {\r\n   (function(i) {\r\n    setTimeout(foo, i*1500, i);\r\n   }(i))\r\n}\r\nfunction foo(i) {\r\n   console.log(\"i:\" + i + \":\" + arr[i]);\r\n\r\n}\r\n\r\nfunction foo2() {\r\n   console.log(\"all done\");\r\n}\r\n\r\n\r\nsetTimeout(foo2, (i)*1500);\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>2) Given a string &#8220;San Francisco Hotel&#8221; and a array of strings (e.g. San Jose Culture of Art, Father Francisco and San Mateo&#8221;, &#8220;Yahoo&#8221;, &#8220;I love Hotel&#8221;) etc find out the best match. For example in above &#8220;Father Francisco and San Mateo is best match since it contains San and Francisco.<\/p>\n<p>You can index all given individual strings. For example &#8220;San&#8221;, &#8220;Jose&#8221;, &#8220;Culture&#8221;, &#8220;Art&#8221;, etc\u00a0 mapping name to index of string where it appears.<\/p>\n<p>San ==&gt; array(0,1)<\/p>\n<p>Jose ==&gt; array (0)<\/p>\n<p>Culture ==&gt; array(0)<\/p>\n<p>Art ==&gt; array(0)<\/p>\n<p>Father ==&gt; array(1)<\/p>\n<p>Francisco ==&gt; array(1)<\/p>\n<p>Mateo ==&gt; array(1)<\/p>\n<p>Hotel ==&gt; array(3)<\/p>\n<p>Then to search for an input string, You can get all the results for each word in the input string and combine the resultant set in a hash which contains which word occurs how many times.<\/p>\n<p>For San Francisco Hotel you will need to show strings : 0,1,3 for sure. However to rank these strings you can use a hash which will show you which word appears more times.<\/p>\n<p>result[1] ==&gt; 2\u00a0 (string at index 1 contains 2 matching words)<\/p>\n<p>result[0] ==&gt; 1 (string at index 0 contains 1 matching word)<\/p>\n<p>result[3] ==&gt; 1\u00a0 (string at index 3 contains 1 matching word)<\/p>\n<p>&nbsp;<\/p>\n<p>3) Given a sorted array return a random array.<\/p>\n<p>4) Given an array of N numbers return the max product of 3 numbers. Example if the array contains [4,2,7,5,3] then the max product possible is 4 * 7 * 5 = 140.<\/p>\n<p>5) In Javascript Using functional programming , given an array of numbers, return the sum of their squares.<\/p>\n<pre>var arr = [2,4,6,8];\r\n\r\nvar sum = arr.map(function(s) { return s*s;}).reduce(function(acc, val) {return acc + val;});\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>1) Write a javascript function to print an array slowly (e.g each element in intervals of 500 ms). Example of array : 1,5,6,&#8217;cool&#8217;,8. Once done then print &#8220;all done&#8221;. Method-1 var arr = [1,5,6,&#8221;cool&#8221;,8]; function foo(i) { console.log(&#8220;i:&#8221; + i + &#8220;:&#8221; + arr[i]); } function foo2() { console.log(&#8220;all done&#8221;); } for (i=1; i &lt;=3 &hellip; <a href=\"http:\/\/www.tech.dimprash.com\/?p=708\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Ten-X Interview<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[15],"tags":[],"class_list":["post-708","post","type-post","status-publish","format-standard","hentry","category-interview-questions"],"_links":{"self":[{"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=\/wp\/v2\/posts\/708","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=708"}],"version-history":[{"count":21,"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=\/wp\/v2\/posts\/708\/revisions"}],"predecessor-version":[{"id":1075,"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=\/wp\/v2\/posts\/708\/revisions\/1075"}],"wp:attachment":[{"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=708"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=708"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=708"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}