-->

Daffodil Interview Solution -Set 1

Posted by Admin on
Que:Write a program to print following output from given input.
Input: aaa bbb ccc ddd eee fff ggg hhh iii jjj
Output: bbb aaa ddd ccc fff eee hhh ggg jjj iii

public class DaffodilStringQuestion {
public static void main(String args[])
{
String input="aaa bbb ccc ddd eee fff ggg hhh";
String temp1="";
String temp2="";
String finalString="";
int flag=0;
int printString=0;
for(int i=0;i<input.length();i++)
{
       if(flag==0 && input.charAt(i)!=' ')
       {
              temp1=temp1+input.charAt(i);
       }
       else if(flag==1 && input.charAt(i)!=' ' )
       {
              temp2=temp2+input.charAt(i);
       }
       else if(flag==1 && (input.charAt(i)==' '))
       {
              printString=1;
              flag=1-flag;
       }
       else
       {
             
              flag=1-flag;
       }
      
       // we have reached to the last character in string
       if(i==input.length()-1)
       {
             
              printString=1;
       }
      
       //reversing two two adjacent substring with each other
       if(printString==1)
       {
             
              finalString=finalString+temp2+" "+temp1+" ";
              temp1="";
              temp2="";
              printString=0;
       }
      
}
System.out.println("output:-"+finalString);
}
}

Que:write a program to print following
************
 ************
  ************
   ************
    ************              
     ************     
     ************
    ************
   ************
  ************
 ************
************


import java.util.Scanner;

public class DaffodilQuestion {
public static void main(String args[])
{
       Scanner in =new Scanner(System.in);
    int n=in.nextInt();
    for(int i=1;i<=n;i++)
    {
       if(i<=n/2)
       {
              for(int k=1;k<i;k++)
              {
                     System.out.print(" ");
              }
       }
       else  
       {
              for(int k=1;k<=(n-i);k++)
              {
                     System.out.print(" ");
              }
       }
   
       for(int j=1;j<=n;j++)
       {
       System.out.print("x");
       }
       System.out.println("\n");
    }
}
}


Que:Write a program for preorder traversal of a tree?


class Node
{
       int data;    
    Node right;
    Node left;
    Node(int d)
    {
       data=d;
       right=null;
       left=null;
    }
}

class BinarySearchTree
{
       public Node insert(Node p,int key)
       {
              if(p==null)
              {
                     p=new Node(key);
              }
              else if(key < p.data)
              {
                     p.left=insert(p.left,key);
              }
              else if(key > p.data)
              {
                     p.right=insert(p.right,key);
              }
             
              return p;
             
       }

       // preorder traversal of binary tree
       public void preorder(Node p)
       {
              if(p!=null)
              {
              System.out.print(p.data+" ");
              preorder(p.left);
              preorder(p.right);
              }
       }

}

public class DaffodilTreeQuestion {

       public static void main(String args[])
       {
              BinarySearchTree bt =new BinarySearchTree();
              Node root=null;
              root=bt.insert(root,5);
              root=bt.insert(root,3);
              root=bt.insert(root,6);
              root=bt.insert(root,1);
              root=bt.insert(root,4);
              // preorder traveral of binary tree
              System.out.println("preorder traversal of binary tree:-");
              bt.preorder(root);
       }
}

Please comment if you find any mistake.


4 comments: