## Monday, May 17, 2010

### Java

- How many classloaders are instantiated in JVM by Default ? Ans: 1 (Primordial ClassLoader) Details: http://www.javaworld.com/javaworld/jw-10-1996/jw-10-indepth.html
- What does volatile keyword in Java do ?

### Design Patterns

- What is the difference between an Adapter and a Facade pattern. Why do you need both of them ?
- Adapter is for altering an interface. Facade is to provide a simplified interface for a complex subsystem
- Adapter hides the altered interface from the client. Facade still exposes the subsystem interface for advanced clients.
- How does decorator differ from Subclassing + overriding ?
- What is double checking strategy in Singleton pattern ?
- Declare the single instance to be volatile.
- Synchronize block for creating instance.
- Check for instance null just before entering and just after entering Synchronized block before creating var.
- How do we circumvent the problem of a class being Singleton across classLoaders also ?

### Puzzles

- How many cars are there in USA ? (Similar .. Television sets)
- Party > n number of people are wearing white hats. Each can see others hat. Not their own. Everytime gatekeeper opens door to balcony and calls for white hat people to go up to balcony. How many calls is needed for all white hat people to leave the waiting room.
- Ans : n Calls are required. Mathematical induction.
- You've got someone working for you for seven days and a gold bar to pay them. The gold bar is segmented into seven connected pieces. You must give them a piece of gold at the end of every day. If you are only allowed to make two breaks in the gold bar, how do you pay your worker?
- Ans: Divide into 2,1,4: 1st day: 1 piece, 2nd day: Take 1, Give: 2, 3rd day: Give 1, 4th day: Take 2,1 Give 4...go on
- One train leaves Los Angeles at 15mph heading for New York. Another train leaves from New York at 20mph heading for Los Angeles on the same track. If a bird, flying at 25mph, leaves from Los Angeles at the same time as the train and flies back and forth between the two trains until they collide, how far will the bird have traveled?
- Ans: Find the time for the Trains to collide. Say T. Then bird travelled: 25XT
- Imagine you are standing in front of a mirror, facing it. Raise your left hand. Raise your right hand. Look at your reflection. When you raise your left hand your reflection raises what appears to be his right hand. But when you tilt your head up, your reflection does too, and does not appear to tilt his/her head down. Why is it that the mirror appears to reverse left and right, but not up and down?
- Ans: Draw a ray diagram to explain lateral inversion
- You have a bucket of jelly beans. Some are red, some are blue, and some green. With your eyes closed, pick out 2 of a like color. How many do you have to grab to be sure you have 2 of the same?
- Ans : 4 (Ref: Pigeon Hole Problem)
- There are four dogs/ants/people at four corners of a square of unit distance. At the same instant all of them start running with unit speed towards the person on their clockwise direction and will always run towards that target. How long does it take for them to meet and where?
- Answer :
**How ?**. They will meet in the center and the distance covered by them is independent of the path they actually take (a spiral).

### Complexity Of Algos

- N^2 vs n log n http://www.lump.org/wp-content/uploads/2008/12/untitled.png
- why sorting requires n log n minimum ? http://www.cs.colorado.edu/~karl/2270.spring03/sorting.html

### Algorithm questions

- Nth largest number in an array (Selection sort descending O(n^2)): Better method. Find the pivot and look at it's position. Details: Learn QuickSort (Complexity: O(n))
- Nth smallest number in an array (Selection sort ascending) Same as above
- How would you validate an XML document without using any APIs
- given two string arrays find if they contain same strings i.e {"a","b","ab"} and {"ab","ab","a","b"} are same but {"a","b"} and {"ab"} are different
- Design a data structure that has order(1) time complexity for insert, update,delete, ispresent(key) operations
- Implement Find and Delete for binary tree.
- Given two sorted postive integer arrays A(n) and B(n) (W.L.O.G, let's say they are decreasingly sorted), we define a set S = {(a,b) | a \in A and b \in B}. Obviously there are n^2 elements in S. The value of such a pair is defined as Val(a,b) = a + b. Now we want to get the n pairs from S with largest values.

Subscribe to:
Posts (Atom)