Ten-X Interview

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,’cool’,8. Once done then print “all done”.

Method-1

var arr = [1,5,6,"cool",8];

function foo(i) {
   console.log("i:" + i + ":" + arr[i]);

}

function foo2() {
   console.log("all done");
}

for (i=1; i <=3 ; i++) {
    setTimeout(foo, i*500, i);
}

setTimeout(foo2, (i)*500);

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.

Method-2

var arr = [1,5,6,"cool",8];
var i = 0;

function foo() {
   console.log("i:" + i + ":" + arr[i]);
   i++;
   if (i > arr.length) {
       console.log("all done");
   } else {
       setTimeout(foo, 500);
   }
   
}

setTimeout(foo, 500);

Method-3

const arr = [10, 12, 15, 21];
for (let i = 0; i < arr.length; i++) {
  // using the ES6 let syntax, it creates a new binding
  // every single time the function is called
  // read more here: http://exploringjs.com/es6/ch_variables.html#sec_let-const-loop-heads
  setTimeout(function() {
    console.log('The index of this number is: ' + i);
  }, 3000*i);

Method-4

for (i = 0, max = 9; i < max ; i ++) {
  setTimeout(printFunc(i), 500*i);
}

function printFunc(i) {
   return function() {
       console.log(i);
   }
}

Method-5. (Using IIFE – Immediately Invoked Function Expression)

var arr = [1,5,6,"cool",8];

for (i=0; i < arr.length ; i++) {
   (function(i) {
    setTimeout(foo, i*1500, i);
   }(i))
}
function foo(i) {
   console.log("i:" + i + ":" + arr[i]);

}

function foo2() {
   console.log("all done");
}


setTimeout(foo2, (i)*1500);

 

2) Given a string “San Francisco Hotel” and a array of strings (e.g. San Jose Culture of Art, Father Francisco and San Mateo”, “Yahoo”, “I love Hotel”) etc find out the best match. For example in above “Father Francisco and San Mateo is best match since it contains San and Francisco.

You can index all given individual strings. For example “San”, “Jose”, “Culture”, “Art”, etc  mapping name to index of string where it appears.

San ==> array(0,1)

Jose ==> array (0)

Culture ==> array(0)

Art ==> array(0)

Father ==> array(1)

Francisco ==> array(1)

Mateo ==> array(1)

Hotel ==> array(3)

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.

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.

result[1] ==> 2  (string at index 1 contains 2 matching words)

result[0] ==> 1 (string at index 0 contains 1 matching word)

result[3] ==> 1  (string at index 3 contains 1 matching word)

 

3) Given a sorted array return a random array.

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.

5) In Javascript Using functional programming , given an array of numbers, return the sum of their squares.

var arr = [2,4,6,8];

var sum = arr.map(function(s) { return s*s;}).reduce(function(acc, val) {return acc + val;});