Friday, February 29, 2008

Ajax 101

­        --  The name Ajax is short for Asynchronous JavaScript and XML, and it's made up of several components

o        Browser-based presentation using HTML and CSS

o        Data stored in XML format and fetched from the server

o        Behind-the-scenes data fetches using XMLHttpRequest objects in the browser

o        JavaScript to make everything happen

­        --  How Ajax works

o        JavaScript code can fetch data from the server as needed

o        When more data is needed from the server, the JavaScript uses a special object supported by browsers, the XMLHttpRequest object, to send a request to the server behind the scenes – without causing a page refresh

o        The JavaScript in the browser doesn't have to stop everything to wait for that data to come back from the server. It can continue with other tasks and wait for the data in the background – that's called asynchronous data retrieval

o        The data that comes back from the server can be XML, or just plain text. The JavaScript code in the browser can read that data and put it to work immediately

­           

­         -- The XMLHttpRequest object is usually part of the browser's window object, so to check whether XMLHttpRequest is ready to use, we can use the following code

var XMLHttpRequestObject = null;

if(window.XMLHttpRequest)

XMLHttpRequestObject = new XMLHttpRequest();

­         -- If the browser doesn't support XMLHttpRequest, but supports ActiveX, we can add an else part to the condition

else {

XMLHttpRequestObject = new ActiveXObject("Microsoft.XMLHTTP");

}

­           

­         -- To configure the object to use an URL we can use the open() method of the object

XMLHttpRequestObject.open("GET", "http://test.com/data");

­        --  The open() method can take the following parameters

o        method – the HTTP method used to open the connection, such as GET, POST, PUT, HEAD or PROPFIND

o        URL – the requested URL

o        asyncFlag – a Boolean value indicating whether the call is asynchronous. The default is true

o        userName – the user name

o        password – the password

­         -- The open() method has the following format

open("method", "URL"[, asyncFlag[, "username"[, "password"]]])

­        --  When using Ajax, we usually use GET when we want to retrieve data, and POST when we want to send a lot of data to the server

­         -- It is not necessary that the URL we use with the open() method should be an absolute URL, it can also be a relative URL

­        --  By default, the connection to this URL is made asynchronously, which means that this statement doesn't wait until the connection is made and the data is finished downloading

­         -- When we call the open() method it doesn't actually make the connection

­        --  Only when the send() method is called that the connection is made

­        --  But before that we need to setup the object to handle the data received from the URL

­           

­        --  The XMLHttpRequest object has a property named onreadystatechange that lets us handle asynchronous loading operations

­        --  If we assign the name of a JavaScript function in the script to this property, that function will be called each time the XMLHttpRequest object's status changes – as when it's downloading data

­        --  We can use the JavaScript shortcut to create an anonymous function on the fly and assign it to this property

XMLHttpRequestObject.onreadystatechange = function() {

      //code to handle the XMLHttpRequest object's status changes

}

­         -- This new anonymous function will be called when the XMLHttpRequest object undergoes some change

­         -- We need to watch two properties of the object to determine the download is complete

o        readyState – tells us how the data loading is going.

The possible values are

§         0 – initialized

§         1 – loading

§         2 – loaded

§         3 – interactive

§         4 – complete

o        status – it holds the status of the download itself. This is the standard HTTP status code that the browser got for the URL that we supplied

200 means everything is fine.

Some of the possible values

§         200 – OK

§         204 – No content

§         400 – Bad Request

§         401 – Unauthorized

§         403 – Forbidden

§         404 – Not Found

§         500 – Internal Server Error

§         505 – Http Version Not Supported

­         -- To make sure the data we want has been downloaded completely and everything went okay, we need to make sure that the XMLHttpRequest object's readyState property equals 4 and the status property equals 200

­        --  If this condition is true, then we can know that the data downloading went fine and is complete now, and the data is ready for use now

­           

­         -- We can get the data from the XMLHttpRequest object in two ways

o        If the retrieved data has to be treated as standard text – use the object's responseText property

o        If the data has been formatted as XML – use the objects responseXML property

­         -- To make the data appear in the web page, we can assign it to some element in the page

XMLHttpRequestObject.onreadystatechange = function() {

      if(XMLHttpRequestObject.readyState == 4 &&

                        XMLHttpRequestObject.status == 200) {

            obj.innerHTML = XMLHttpRequestObject.responseText;

      }

}

­           

­         -- After setting up the code to handle the response from the server, we actually make the request

­         -- To make a request, we use the send() method

­         -- When we're using the GET method, we send a value of null

XMLHttpRequestObject.send(null);

­        --  The call to send() is what actually downloads the data so that the anonymous function can handle that data

­           

­        --  XMLHttpRequest offers only responseText and responseXML properties

­        --  We can only download text using Ajax request, there is no way to download binary data using XMLHttpRequest

­         -- XMLHttpRequest object of IE has a responsesStream property, which represents a binary data stream from the server

­        --  But JavaScript cannot handle a binary stream, but can be handled by other scripting languages like VBScript

Thursday, February 28, 2008

Microsoft and Google Interview Questions

The following questions were asked in Microsoft and Google interviews.

1) You are given with two arrays A and B.
A has integers unsorted.
B has the same length as A and its values are in the set {-1,0,1}
You have to return an array C with the following processing on A.
If B has 0 then C must have A
If B has -1 then A must be in C within the sub-array C[0] - C[i-1] i.e. Left sub array
If B has 1 then A must be in C within the sub array C[i+1] - C[length(A)] i.e. right sub array.
If no such solution exists then print "no solution"

2) consider a binary tree, Policemen is to be placed such that for each edge of the tree, there is a policemen on at least one side of each edge.
Tell the minimum no. of policemen and their location. (time-O(N)).

3) You are given an expression like (a+ ((b+c)))..... Here extra braces are provided on b+c... i mean like (b+c) is ok... bt ((b+c)) is not as it contains redundant braces. So for a given expression you have to find whether expression contains redundant braces or not.....
(a+(b+c) )is ok and (a+((b+c)) contains redundant one. can we solve by counting number of operands ,number operators and number of braces. Using stack can we solve this problem?

4) Prove that number of prime numbers is infinite. I.e. there is no an upper limit after which prime number doesn't exist. Prove that.


5) See the below matrix.
a b c d
e f g h
i j k l
m n o p

here we define a term contiguous.
(a,b) are contiguous.
(a,e) are contiguous.
(a,f) are contiguous.
Butt (a,j) are not contiguous.

You are given an N X N matrix/array with distinct values.
You are also given m values. Now u have to detect in above provided array that whether these m values r contiguous or not.

Like in above matrix.
abcd are contiguous.....
bfjn are contiguous.....
bfkh are contiguous.....
cfjg are contiguous.....
dlpk are not contiguous.....
I.e. m values are contiguous if u can reach all m values.
 
(Source: Orkut)

Tuesday, February 26, 2008

Static Import

When a set of constants are tightly tied with a set of classes or package - the normal method used is to define the constants in an interface and implement that interface in the class. The advantage is that we can refer to the constants without qualifying them with a class name, like - Math.PI.
 
But this is a bad idea. The Constant Interface Antipattern (Effective Java, Item 17) explains the drawbacks:
(1) it becomes part of the class API, which can confuse the eventual users of the class
(2) all the subclasses will unnecessarily have the interface as part of their inheritance tree
 
Workaround is to use a static import.
 
The static import construct allows unqualified access to static members without inheriting from the type containing the static members. We inherit the static members either individually
 
    import static java.lang.Math.PI;
 
or en masse:
 
    import static java.lang.Math.*;
 
Once imported, the static members can be used without qualification:
 
    double r = cos(PI * theta);
 
We can also import static methods individually
 
    import static java.lang.Math.random;
 
The static import declaration is analogous to the normal import declaration. Where the normal import declaration imports classes from packages, allowing them to be used without package qualification, the static import declaration imports static members from classes, allowing them to be used without class qualification.
 
We should take care that this feature is used sparingly, otherwise the code will become unreadable, making it difficult to trace which class a static member belongs to. Also, if only one or two members are required, it is advisable to import them individually, rather than importing all the static members.
 
(http://java.sun.com/j2se/1.5.0/docs/guide/language/static-import.html)

Tuesday, February 12, 2008