Welcome Message

Hi, welcome to my website. This is a place where you can get all the questions, puzzles, algorithms asked in interviews and their solutions. Feel free to contact me if you have any queries / suggestions and please leave your valuable comments.. Thanks for visiting -Pragya.

December 29, 2011

Strategy Pattern

Key points :

1. Also known as Policy pattern
2. Collection of Algos (Behavuours)
3. Algo can be selected at runtime
4. Uses composition instead of inheritance

e.g. a class used for validation of incoming data may use strategy pattern in which the validation algo will be decided on the basis of data set

June 20, 2011

LimeSpot interview Question (Telephonic Interview)

Currently there are three projects in LimeWire :
1. LimeWire
2. Lime Domain : Hosts domain for users
3. LIme Exchange

Q1: What in an outer join and what is an Inner join?
A: Inner Join returns a row when there is at-least one match in one match in both the tables. e.g.
SELECT Persons.LastName, Persons.FirstName, Orders.OrderNo
FROM Persons
INNER JOIN Orders
ON Persons.P_Id=Orders.P_Id
ORDER BY Persons.LastName

http://www.w3schools.com/sql/sql_join_inner.asp

Outer JOin :
Returns a row even if there is no match.
An outer join does not require each record in the two joined tables to have a matching record. The joined table retains each record—even if no other matching record exists. Outer joins subdivide further into left outer joins, right outer joins, and full outer joins, depending on which table(s) one retains the rows from (left, right, or both).

Q2. What is Normalisation and what is BCNF?
A: Boyce Codd Normal Form

March 13, 2011

Volatile variable in java

According to Kathy Sierra :

" The volatile modifier tells the JVM that a thread accessing the variable must
always reconcile its own private copy of the variable with the master copy in
memory "

My understanding :
In normal scenario, In a multi-threaded environment, every thread keeps a local copy of instance variable and if a thread makes some change to the variable, the changed value would be visible to that thread only.

But if we declare a variable as volatile, the variable is stored in main memory and not in local memory of any thread. So, if the value of the variable is changed by any thread, it will be changed in the main memory and would be visible to all threads accessing that variable.

March 9, 2011

The Java Class Loading Mechanism


The Java platform uses a delegation model for loading classes. The basic idea is that every class loader has a "parent" class loader. When loading a class, a class loader first "delegates" the search for the class to its parent class loader before attempting to find the class itself.

Constructors in java.lang.ClassLoader and its subclasses allow you to specify a parent when you instantiate a new class loader. If you don't explicitly specify a parent, the virtual machine's system class loader will be assigned as the default parent.
The loadClass method in ClassLoader performs these tasks, in order, when called to load a class:
If a class has already been loaded, it returns it.
Otherwise, it delegates the search for the new class to the parent class loader.
If the parent class loader does not find the class, loadClass calls the method findClass to find and load the class.
The findClass method of ClassLoader searches for the class in the current class loader if the class wasn't found by the parent class loader. You will probably want to override this method when you instantiate a class loader subclass in your application.
The class java.net.URLClassLoader serves as the basic class loader for extensions and other JAR files, overriding the findClass method of java.lang.ClassLoader to search one or more specified URLs for classes and resources.

forward vs sendRedirect

RequestDispatcher.forward() :

1) Forwards the request to some other servlet/jsp within the same application.
2) The URL in the browser does not change so the client does not get to know that the request is being sent to some other servlet. The process occurs completely within the web container
3) Request attributes are maintained
4) Faster, since everything is at the server side only

Response.sendRedirect()
1) Can forward request to a resource in some other application as well
2) The URL in the browser changes. So the web container returns to the browser indicating that the a new URL should be requested.
3)  Since a new request object is created, any request attributes already stored will be destroyed.
4) Slower, since the round trip to client is involved

java.lang.Error

Error extends Throwable.

It indicates that some serious and abnormal condition has occurred and the application should not try to catch it.

March 4, 2011

Multiple catch blocks



Both

try{

     }catch(Error e){

     }catch(NullPointerException npe){

    }

and


           try{

}catch(NullPointerException npe){

}catch(Error e){

}

will compile.

Exception Hierarchy



Throwable is a class which extends Serializable interface.
All other classes in hierarchy extend Throwable class

Error , Exception and Runtime Exception classes only have the constructors (which call the corresponding super class constructors). All other methods are inherited from the Throwable class itself.

January 25, 2011

HTTP Status Codes

HTTP Status Codes represent the status of response received by Client from the Server

Overall range
Defined range
Category
100-199
100-101
Informational
200-299
200-206
Successful
300-399
300-305
Redirection
400-499
400-415
Client error
500-599
500-505
Server error

January 23, 2011

Struts - Controller


In a Struts application, two components make up the Controller. These two components are the
org.apache.struts.action.ActionServlet and the org.apache. struts.action.Action classes. In most Struts
applications, there is one org. apache.struts.action.ActionServlet implementation and many org.apache.
struts.action.Action implementations. ActionServlet acts as an Action factory by creating specific Action classes based on the user’s
request.


The org.apache.struts.action.ActionServlet is the Controller component that handles client requests and
determines which org.apache.struts.action.Action will process the received request. When assembling simple
applications, the default ActionServlet will satisfy your application needs,
and therefore, you do not need to create a specialized org.apache.struts.action.ActionServlet implementation.

Also, we generally use Action Mappings as *.do, but we do not append that in URL. This is because when we use , .do is automatically appended. 


January 22, 2011

WebApps


Web applications allow compiled classes to be stored in both the /WEB−INF/classes and /WEB−INF/lib
directories. Of these two directories, the class loader will load classes from the /classes directory first,followed by the JARs in the /lib directory. If you have duplicate classes in both the /classes and /lib
directories, the classes in the /classes directory will take precedence.

January 9, 2011

Manage ( Enable / Disable ) startup programs in Windows Vista

You can Enable / Disable startup programs on your PC with Windows Vista by following the following simple steps :

1. Go to Control Panel  -> Windows Defender



2. Then go to Tools and then Software Explorer (as shown in the image below)

3. Make sure that the Category selected is 'Startup Programs'.

Then select the program that you want to Disable or Remove and click the corresponding button(as shown below).





Servlet load-on-startup

The value of the tag load-on-startup helps the container determine when the servlet has to be loaded.

A value >=0 means that the servlet has to be loaded when the application is deployed.

If the value is less than zero or if the tag element is not present, the servlet will be loaded when it is first called.

Also, the order in which servlets are loaded is determined by the value of load-on-startup. The servlet which has a lower load-on-startup value will be loaded first. e.g the servlet with value 2 will be loaded before the servlet with this value as 4.

Apart from this, if two servlets have same value of load-on-startup, then they are loaded in the order in which they are defined in the Deployment descriptor file.

January 4, 2011

An Unusual Paragraph

This is a most unusual paragraph. How quickly can you find out
what is so unusual about it? It looks so ordinary, you'd think
nothing was wrong with it and in fact, nothing is wrong with it.
It is unusual, why? Study it. Think about it and you may find
out. If you work at it for a bit,
it will dawn on you. Try to do it without coaching.


January 3, 2011

World War - I Puzzle

A grandfather is telling his grandson stories of world war.

He said :

"At the end of World War 1, I was awarded for my bravery
after saving a group of my men"

"We were fighting in northern France and one of our
enemies threw a grenade at us. I managed to pick it up and
throw it away before it exploded. So right after the war
ended, a General gave me a sword, engraved with the words
'Awarded for Bravery and Valor, A True Hero, World War 1'".

"The grandson thinks about the story for a minute and then
says "Grandpa, that story can't be true!"

How did the grandson know?