Jun 10, 2014

Cookie

Cookie is a small piece of data sent from a server and stored in the browser. Browser sends cookie back with each subsequent request based on set of rules. Few of the common examples

ASP.NET_SessionId is a cookie that ASP.NET uses to store a unique identifier for your session.

   Set-Cookie: ASP.NET_SessionId=huilln20biy333vr3smug2sb; path=/; HttpOnly


This is set only when user tries to store something to the session, for example in asp.net mvc if we add this code, then we should see this cookie.
ControllerContext.HttpContext.Session.Add("a",1);

.ASPXAUTH is a cookie that asp.net uses for form authentication.

     Set-Cookie: .ASPXAUTH=095F6C2AF0126AF84BD5A30AD2866328E06F61755EA6FCDEDAA5A79F9039FB38AC4812628A42C700B7E927B58CA6B50F831DA2143A06385AA422ED313CB39303C3C0DA75DCFE9BCF363B7969FCFC6B0114D362CE6C1A04C424C7B1D46A440170B1DABD47E6DD8C91D6EE64B74F5224B6; path=/; HttpOnly

A web server specifies a cookie to be stored by sending an HTTP header called Set-Cookie. This is how response header looks.

     Set-Cookie: value[; expires=date][; domain=domain][; path=path][; secure]
     Set-Cookie: MyCompany=SessionKey={some unique id}&UserName=MyName&UserId=MyId; domain=.somewhere.com; path=/

In asp.net mvc you can use following code in order to perform set this header

        var httpCookie = new HttpCookie("MyCompany", "SessionKey=something&UserName=MyName");
        httpCookie.Values["UserId"] = "MyId";
    
 httpCookie.Expires = DateTime.Today.AddDays(1);
 httpCookie.Domain = "bogus.com";
 httpCookie.Path = "/product";
 httpCookie.Secure = true;
 ControllerContext.HttpContext.Response.Cookies.Add(
  httpCookie
  );


Value is string in the format name=value. In the above example we have used subcookies in order to increase the number as there is limitation on number of cookie.

Value is sent to the server with each subsequent request if option allows

     Cookie: MyCompany=SessionKey={some unique id}&UserName=MyName&UserId=MyId;

Each of the options after cookie value are separated by semicolon and space.

Expires option indicates when the cookie expires and should not be sent back to the server. Without the expires option, a cookie has a lifespan of a single session as we saw in case of ASPXAUTH,ASP.NET_SessionId

Domain options indicates domains for which cookie should be sent. By default domain is set to the host name of the page setting the cookie. This is useful for case like mail.somewhere.com and finance.somewhere.com. By setting .somewhere.com cookie can be shared these sets of sites. Browser performs a trail comparison of this value and the host name to which a request is sent.

Path options is another way to control cookie. This comparison is done by comparing the option value character-by-character against the start of the request URL. If the characters match, then the Cookie header is sent.

If secure option is specified then cookie will only be sent to the server when a request is made using SSL and the HTTPS protocol.

Jun 9, 2014

Knockout

Knockout.js is a javascript library that allows two ways bindings between html elements and data model. This means any changes to data model can be automatically reflected in DOM and vice-versa. It simplifies dynamic javascript ui with MVVM pattern.

Observables are Functions
This was little confusing in beginning and probably I spend a lot of time in order to understand this.

var personModel = {
    firstName: ko.observable(''),
    lastName: ko.observable(''),
}

This is how you access and set these properties.

var firstName = personModel.firstName();
personModel.lastName('Raj');

Observable Arrays Track Arrays, Not the Items Within
An observableArray also has some extra methods added to it to perform basic array operations-pop, push, shift, unshift, reverse, sort, splice,remove,removeall.... Refer this for complete list

ko.toJS
You get a plain copy that contains only your data and no Knockout-related artifacts

ko.toJSON
This can be used for debugging too, something like this.
<hr />
<h2>Debug</h2>
<div data-bind="text: ko.toJSON(viewModelCart)"></div>

Manual Subscription
In typical scenarios you won't need manual subscription but sometimes it becomes handy. Consider an example where you have drop-down and based on the drop down selection, you want to make an ajax call, so you can write something like this.
viewModel.selectedItem.subscribe(function(newValue) {
    //make ajax call
});

knockout.mapping plugin.
Sometime using mapping plugin saves a lot of time. You can use ignore/include to have more control while mapping.

computedObservables

knockout.validation
Very handy in terms of applying validation logic.

Adding static text on data binding
This will not work
data-bind="text: '$' + Amount"

you have to use
data-bind="text: '$' + Amount()"
or 
data-bind="text: '$' + ko.utils.unwrapObservable(Amount)"

Jun 4, 2014

Sorting Algorithm

Linear
Divide and Conquer

Performance Measure
Number of swaps
Number of comparison

Bubble Sort
  • Compare each array item to it’s right neighbor
  • If the right neighbor is smaller then Swap right and left
  • Repeat for the remaining array items
  • At the end of first iteration you will have largest number at the end
  • For next iteration you don't have to consider last item for comparison, since it is already at the correct place.
  • Follow above step until you perform no swap.
  • Performance
    • Worst Case: O(n2) - Not suitable for large unsorted data
    • Average Case: O(n2) - Not suitable for large unsorted data
    • Best Case : O(n) - Works best for already sorted or nearly sorted data
    • Space Required: O(n)
  • Reference

Insertion Sort
  • Everything left of the current item being worked on is considered to be sorted
  • If current item is greater than the last item in the sorted part, then current item is at the correct position.
  • If above is not true then you find the correct position of the current item in the sorted part and then insert it at that location after shifting rest of shorted items by one towards right.
  • Follow above step until end.
  • Only one iteration
  • Performance
    • Worst Case: O(n2) - Not suitable for large unsorted data
    • Average Case: O(n2) - Not suitable for large unsorted data
    • Best Case : O(n) - Works best for already sorted or nearly sorted data
    • Space Required: O(n)
  • Reference

Selection Sort
  • Enumerate the array from the first unsorted item to the end
  • Identify the smallest item
  • Swap the smallest item with the first unsorted item
  • Performance
    • Worst Case: O(n2) - Not suitable for large unsorted data
    • Average Case: O(n2) - Not suitable for large unsorted data
    • Best Case : O(n2) - Not suitable for large unsorted data. This does a lot of comparison because of that this has worst performance in best case scenarios also.
    • Space Required: O(n)
  • Reference

Merge Sort
  • The dataset is recursively split in half until it contains one item.
  • It is then merged in sorted order
  • After this point sets are always sorted
  • This is called divide and conquer
  • Performance - This has predictable performance O(n log n) in all kind of scenarios which is different that other Linear sorting algorithm. This has also advantage of working in parallel.
    • Worst Case: O(n log n) - Suitable for large unsorted data
    • Average Case: O(n log n) - Suitable for large unsorted data
    • Best Case : O(n log n) - Suitable for large unsorted data
    • Space Required: O(n) - Merge can be, but is often not, performed in-place, meaning new data sets are created during split. These extra allocations increase the memory footprint required to sort data.
  • Reference

Quick Sort
  • Pick a pivot value
  • Move every item small than pivot item towards it left and item greater than pivot item toward its right.
  • At this point you know that pivot item is at the right location. 
  • Perform this operation on items on both side of the pivot until everything is sorted
  • Performance
    • Worst Case: O(n2) - Not suitable for large inverse sorted data
    • Average Case: O(n log n) - Suitable for average case
    • Best Case : O(n log n) - Works best for already sorted or nearly sorted data
    • Space Required: O(n)
  • Reference