Trulia On Site Questions

1) How does Oauth work ?

2) Explain journey of a URL ?

3) How does caching work in a CDN? How do you push a new file out to CDN?

4) Emulate the throttle function()

// Allow callback to run at most 1 time per 100ms
window.addEventListener("resize", throttle(callback, 500));
// Allow callback to run on each resize event
window.addEventListener("resize", callback2);

function callback ()  { console.count("Throttled");     }
function callback2 () { console.count("Not Throttled"); }

function throttle (callback, limit) {
    var wait = false;                  // Initially, we're not waiting
    return function () {               // We return a throttled function
        if (!wait) {                   // If we're not waiting
            callback.call();           // Execute users function
            wait = true;               // Prevent future invocations
            setTimeout(function () {   // After a period of time
                wait = false;          // And allow future invocations
            }, limit);
        }
    }
}

How to achieve static variable in javascript

2 Ways :

1) Make use of the fact that functions are objects and can have properties
(Even using “this” instead of uniqueID inside the function will do)

function uniqueID() {
    // Check to see if the counter has been initialized
    if ( typeof uniqueID.counter == 'undefined' ) {
        // It has not... perform the initialization
        uniqueID.counter = 0;
    }
    uniqueID.counter++;
    return (uniqueID.counter);
}

console.log(uniqueID());
console.log(uniqueID());


2) Using closure and function expressions

var uniqueID = (function() {
   var id = 0; // This is the private persistent value
   // The outer function returns a nested function that has access
   // to the persistent value.  It is this nested function we're storing
   // in the variable uniqueID above.
   return function() { return id++; };  // Return and increment
})(); // Invoke the outer function after defining it.

console.log(uniqueID());
console.log(uniqueID());


Given Array of positive negative numbers find 2,3 of them that sum to zero

Given an array of numbers with positive and negative, find pairs that sum to zero  
a) sort the numbers
b) start 2 pointers from from begin and end
c) if sum is > or < = then move appropriate pointer. Above question, but now finding 3 numbers which sums to zero. a) for each number in array as target (index:0, 1, 2, etc) start 2 pointers from i+1 and len-1 if sum of target + 2 pointers is > 0 decrement second pointer
if sum of target + 2 pointers is < 0 increment first pointer

Find if an integer is a prime number

The algorithm can be improved further by observing that all primes are of the form 6k ± 1, with the exception of 2 and 3. This is because all integers can be expressed as (6k + i) for some integer k and for i = ?1, 0, 1, 2, 3, or 4; 2 divides (6k + 0), (6k + 2), (6k + 4); and 3 divides (6k + 3). So a more efficient method is to test if n is divisible by 2 or 3, then to check through all the numbers of form 6k ± 1.

function isPrime($n)
{
    // Corner cases
    if ($n <= 1)  return false;
    if ($n <= 3)  return true;

    // This is checked so that we can skip
    // middle five numbers in below loop
    if ($n % 2 == 0 || $n % 3 == 0) return false;

    for ($i=5; $i*$i<=$n; $i=$i+6) {
        if ($n % $i == 0 || $n % ($i+2) == 0)
           return false;
    }

    return true;
}

Trulia Phone Interview

PHP

1) Difference between interface and abstract class. Write code to show example of each. Can a class derive from abstract class and implement an interface as well? (Yes)

2) Write a function to test a palindrome.

Javascript :
1) What is the difference between
x = 1;
var x = 1;
window.x = 1;

2) Write a function to add an array of numbers using the arguments variable

var data = [1,2,3,4];
console.log(sum(data));

function sum() {
   var myData = arguments[0];
   return myData.reduce(function (acc, num) { acc += num; return acc});
}

3) write a function to implement mult(4)(5)

function mult(x) {
   return function(y) {
       return x*y;
   }
}

var ans = mult(4)(5);

4) Write a function to output alert(1), alert(2) every 1 second till 5 times
Similar to question asked in Ten-X interview.

5) write a function to put spaces in a string “Hello World” ==> “H e l l o W o r l d”.

var spacify = function (str) {
    return str.split('').join(' ');
};

console.log(spacify("Hello"));

Is it possible to write a function spacify like “hello world”.spacify()

String.prototype.spacify = String.prototype.spacify || function() {
  return this.split('').join(' ');
};

console.log("Hello".spacify());