Monday, May 17, 2010

Open Ended

How will you troubleshoot why your website is not performing


  1. How many classloaders are instantiated in JVM by Default ? Ans: 1 (Primordial ClassLoader)  Details:
  2. What does volatile keyword in Java do ? 

Design Patterns

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


  1. How many cars are there in USA ? (Similar .. Television sets)
  2. 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. 
    1. Ans : n Calls are required. Mathematical induction. 
  3. 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? 
    1. 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 
  4. 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?
    1.  Ans: Find the time for the Trains to collide. Say T. Then bird travelled:  25XT
  5. 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? 
    1. Ans: Draw a ray diagram to explain lateral inversion
  6. 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?
    1. Ans : 4  (Ref: Pigeon Hole Problem)
  7. 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?
    1. Answer : How ? . They will meet in the center and the distance covered by them is independent of the path they actually take (a spiral). 


Give a very good method to count the number of ones in a "n" (e.g. 32) bit number.

String manipulations

  1. Reverse a Character array in place in Order of n.

Complexity Of Algos

Algorithm questions

  1. 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))
  2. Nth smallest number in an array (Selection sort ascending) Same as above
  3. How would you validate an XML document without using any APIs
  4. 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
  5. Design a  data structure that has order(1) time complexity for insert, update,delete, ispresent(key) operations
  6. Implement Find and Delete for binary tree.
  7. 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.