Tuesday 12 November 2013

Some Useful Data structure interview questions

Remove the leave Node

struct Node * romoveLeafList(struct Node * root){
    if(root==NULL) return NULL;
    if(root->left==NULL && root->right==NULL){
        return NULL;
    }
    root->left=romoveLeafList(root->left);
    root->right=romoveLeafList(root->right);
    return root;
 
}


Remove all the Node which has sum is less than k 

struct Node * sumofnode(struct Node *root, int k, int *sum)
{
    if(root==NULL)return NULL;
    int leftsum=*sum+root->data;
    int rightsum=leftsum;
    root->left=sumofnode(root->left,k,&leftsum);
    root->right=sumofnode(root->right,k,&rightsum);
    *sum=leftsum>rightsum?leftsum:rightsum;
    if(*sum<k)
    return NULL;
    return root;
}

Wednesday 6 November 2013

Compile and Run java program using another java program (Java Reflection)

CompileRun.java
import java.io.*;
import java.lang.reflect.*;
public class CompileRun
{
 public static void main(String arg[])throws Exception
 {
  System.out.println(“Helo”);
  new com.sun.tools.javac.Main().compile(new String[]
{“HelloWorld.java”});
  Class c=Class.forName(“HelloWorld”);
  Object o=c.newInstance();
  Object ar[]=new Object[]{};
  Class cl[]=new Class[]{};
  Method m=c.getDeclaredMethod(“print”,cl);
  m.invoke(o,ar);
 
 
 }
}

HelloWorld.java 
public class HelloWorld
{
 public void print()
 {
  System.out.println(“Hello World”);
 }
}
Classpath
1)  %classpath%;Q:\SDE7_Tomcat6_JDK6\Java\jdk1.6.0_24\lib\*
2) javac CompileRun.java
3) java -cp “Q:\SDE7_Tomcat6_JDK6\Java\jdk1.6.0_24\lib\*;.” CompileRun

Tuesday 5 November 2013

Ajax call & json object creation from the response using CQ5

javascript

var response = CQ.utils.HTTP.get("resourcepath.selector.json");
if (CQ.HTTP.isOk(response)){ 
var myObject =CQ.HTTP.eval(response);
alert(myObject.name);
}

selector.json.jsp
{"name":"vimal"}