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

Posted in Algorithms | Leave a comment

How does a cache work?

https://en.wikipedia.org/wiki/Cache_(computing)

Posted in Concepts | Leave a comment

How does Oauth-2 work ?

https://www.digitalocean.com/community/tutorials/an-introduction-to-oauth-2

Posted in Concepts | Leave a comment

Implement a HashMap

http://robl.co/implement-your-own-hashmap/

Posted in General | Leave a comment

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;
}
Posted in Algorithms | Leave a comment

Trees: Find count of unival or single valued subtrees

http://www.geeksforgeeks.org/find-count-of-singly-subtrees/

Posted in Algorithms | Leave a comment

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());
Posted in Interview Questions | Leave a comment

Interview Questions for Javascript

https://github.com/h5bp/Front-end-Developer-Interview-Questions#performance-questions

https://www.sitepoint.com/5-typical-javascript-interview-exercises/

Posted in Javascript | Leave a comment

Memcached vs APC vs Varnish

Alternative PHP Cache (APC)
APC provides two caching mechanisms.

APC caches the opcode generated during the PHP execution cycle and then later will be able to avoid loading and parsing PHP files repeatedly.
APC also provides a key-value, object cache for PHP applications in shared memory.

Memcached
Memcached is a distributed, key-value, object cache in memory. This is similar to the object cache provided by APC but there are some important differences. It’s in-memory, while APC’s object cache is in shared memory. This will make Memcached faster, but will also require the memory allocation for it’s storage. The other major difference is that Memcached is distributed. This means that it runs across multiple servers. For example, if you’re load-balancing your application. Generally, the need for a distributed object cache is why you would use Memcached.

Varnish
Varnish is a caching HTTP reverse proxy. The reverse proxy part means that it sits between your application and the outside world. Visiting your domain will actually connect to Varnish. Varnish will then make the corresponding request to your application and then deliver it to the client. It will cache the results of these requests based on a configuration file you can write in the Varnish Configuration Language.

Varnish is neither an opcode cache like APC’s opcode cache component, nor an object cache like those in APC or Memcached. Varnish operates outside of your application and caches the entire HTTP response such as the whole HTML document returned by your application.

Varnish is extremely effective because it will cache the final resulting HTML document after all PHP or other server side processing is done. This means that when varnish delivers a page from cache, it avoids running the PHP code at all and consequently any database queries involved in generating the document. Delivering a cached page from Varnish is like delivering a static HTML file.

Web Cache: APC vs Memcached vs Varnish

Posted in General | Leave a comment

Towers of Hanoi

https://www.youtube.com/watch?v=q6RicK1FCUs

Karumanchi, pg 37.

Posted in Algorithms | Leave a comment