Thursday, November 6, 2008

Technical Interview Questions: Java & C++

Which sorting algorithm is fastest?
Quicksort, Merge-sort

How much time it will take to find largest number in a array
O(n)

What will happen to the lock object when the thread is in sleep state.
the thread will hold the lock and it will not do anything until it explicitly releases the lock by calling notify or exiting from the synchronized block.

How to make a Java program faster?
one possible answer is use the java's concurrent apis

In a given connection object in a client-server application, there are 2 client session open simultaneously, but the 2nd client is not able to connect, getting connection failure message. Assume that singleton pattern is used here. What might be the problem?
The problem can be the singleton pattern will only work per JVM, assuming the connection object is singleton, the 2 clients might be running in two separate JVMs, which might be causing 2nd client unable to connect with connection failure message.

Write in-order or post order traversal program of a binary tree.
Quick solution is to implement it through stack.
For e.g.,
struct {
struct node* left;
struct node* right;
int value;
}node;

struct node* root;
//initialize with some elements;
//initialize a stack

void traverse(struct node* anode) {

struct node* next;
while ((next=stack.pop())!=null) {
if(next!=null)
{
stack.push(anode);
traverse(next->left);
printf("%d,",anode->value);
traverse(next->right);
}
}

printf("%d,",anode->value);

}


Traversing without recursion, can be done using stack

Sample code:
      public void visit(Node node)
{
 if(!node.visit)
 {
  sb.append(node.val + " ");
  node.visit=true;
  stack.pop();
 }
}

public String traverse(Node node)
{
 stack.push(node);
 while((node=(Node) stack.top())!=null)
 {
  if(node.left == null && node.visit==false)
  {
   visit(node);
  
  }
  else if(!node.left.visit)
  {
   stack.push(node.left);
   continue;
  }
  visit(node);
  if(node.right !=null)
  {
   stack.push(node.right);
  }
 }
 return sb.toString();
}




What are marker interface in Java?
Null interfaces, they do not have any method declarations, used for naming a set of classes. Examples are Serializable, Clonable

What is difference between hashmap and hashtable?

Describe singleton design pattern.