Debounce and Throttle in Javascript

Throttle: the original function be called at most once per specified period.
Example: while window resizing

Debounce: the original function be called after the caller stops calling the decorated function after a specified period.
Example: Validating contents of a text field after using has stopped typing.

Throttling will delay executing a function. It will reduce the notifications of an event that fires multiple times.

Debouncing will bunch a series of sequential calls to a function into a single call to that function. It ensures that one notification is made for an event that fires multiple times.

If you have a function that gets called a lot – for example when a resize or mouse move event occurs, it can be called a lot of times. If you don’t want this behaviour, you can Throttle it so that the function is called at regular intervals. Debouncing will mean it is called at the end (or start) of a bunch of events.

A far-fetched metaphor, but maybe could also help.

You have a friend named Chatty who likes to talk with you via IM. Assuming when she talks she sends a new message every 5 seconds, while your IM application icon is bouncing up and down, you can take the…

Naive approach: check every message as long as it arrives. When your app icon bounces, check. It’s not the most effective way, but you are always up-to-date.
Throttle approach: you check once every 5 minutes (when there are new ones). When new message arrives, if you have checked anytime in the last 5 minutes, ignore it. You save your time with this approach, while still in the loop.
Debounce approach: you know Chatty, she breaks down a whole story into pieces, sends them in one message after another. You wait until Chatty finishes the whole story: if she stops sending messages for 5 minutes, you would assume she has finished, now you check all.

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

Oauth-2 and OpenId Connect

OAuth 2.0 is a authorization framework where a user of a service can allow a third-party application to access his/her data hosted in the service without revealing his/her credentials (ID & password) to the application. OAuth 2.0 is a set of defined process flows for “delegated authorization”.

OpenID Connect is a framework on top of OAuth 2.0 where a third-party application can obtain a user’s identity information which is managed by a service.  OpenID Connect  standardizes the flow for person authentication using OAuth2. OpenId Connect is a set of defined process flows for “federated authentication”.

OAuth 2.0 Terminology:

Resource Owner:

Client:

Resource Server:

Authorization Server:

https://hackernoon.com/demystifying-oauth-2-0-and-openid-connect-and-saml-12aa4cf9fdba

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

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;
}