Programming interview - written tests

Anonymous_Abstract

Well-known member
  • Aug 7, 2018
    6,036
    4,566
    113
    Messier 87
    EMINƎM;24264183 said:
    String a = "thinking";
    String b = "guy";

    a = a + b; //thinkingguy
    b = a.substring(0, a.length()-b.length()); //thingking
    a = a.substring(b.length()); //guy

    System.out.println("a " + a);
    System.out.println("b " + b);

    n = n+b;
    b = n.substring(0,n.length()-b.length());
    n = n.substring((n.length()+b.length())-n.length(),n.length());
    System.out.println("After Swap");

    System.out.println("n = "+n);
    System.out.println("b = "+b);
     

    Anonymous_Abstract

    Well-known member
  • Aug 7, 2018
    6,036
    4,566
    113
    Messier 87
    Complete Code Of Merge Sort

    package com.benz.test;

    public class MergeAlgo {

    public static void main(String[] args)
    {
    MergeAlgo al = new MergeAlgo();
    al.algo();
    }


    public void algo()
    {
    int[] arr= {5,4,3,2,1};


    arr =mergeSort(arr);

    for(int a : arr)
    {
    System.out.print(a+"\t");
    }
    }

    public int[] mergeSort(int[] A)
    {
    int n = A.length;

    if(n<2)
    {
    return A;
    }else {
    int mid = (n-1)/2;
    int nL,nR;
    nL=mid+1;
    nR=n-(mid+1);

    int[] left = new int[nL];
    int[] right = new int[nR];



    for(int i=0;i<=mid;i++)
    {
    left = A;

    }

    for(int i=mid+1;i<n;i++)
    {

    right[i-(mid+1)]=A;
    }


    int[] array = new int[A.length];

    mergeSort(left);
    mergeSort(right);
    array = merge(left,right);

    return array;
    }
    }

    public int[] merge(int[] L,int[] R)
    {
    int[] A = new int[L.length+R.length];

    int nL = L.length;
    int nR = R.length;

    int i,j,k;
    i=j=k=0;

    while(i<nL && j<nR)
    {
    if(L<=R)
    {
    A[k]=L;
    i++;
    }else
    {
    A[k]=R[j];
    j++;
    }
    k +=1;
    }

    while(i<nL)
    {
    A[k]=L;
    i++;
    k +=1;

    }

    while(j<nR)
    {
    A[k]=R[j];
    j++;
    k +=1;

    }

    return A;

    }

    }
     
    Last edited:

    Anonymous_Abstract

    Well-known member
  • Aug 7, 2018
    6,036
    4,566
    113
    Messier 87
    Code Of Quick Sort
    package com.benz.test;

    public class Quick {

    public int partition(int[] arr,int low,int high)
    {
    int pivot = high;
    int l = low-1;

    for(int j=low;j<=high-1;j++)
    {
    if(arr[j]<=arr[pivot])
    {
    l+=1;
    swap(l,j,arr);
    }
    }
    swap(l+1,high,arr);

    return l+1;
    }

    public int[] swap(int x,int y,int[] A)
    {
    int temp = A[x];
    A[x] = A[y];
    A[y] =temp;

    return A;
    }

    public void quickSort(int[] arr,int low,int high)
    {
    if(low>=high)
    {
    return;
    }else {
    int z = partition(arr,low,high);

    quickSort(arr,low,z-1);
    quickSort(arr,z+1,high);
    }
    }
    public void show(int[] arr)
    {
    for(int i : arr)
    {
    System.out.print(i+"\t");
    }
    }
    public static void main(String[] args)
    {
    Quick qu = new Quick();

    int[] arr= {20,10,15,40,50,60,45};
    int high = arr.length-1;
    int low=0;
    qu.quickSort(arr, low, high);

    System.out.println("Sorted Array");
    qu.show(arr);


    }

    }
     

    Anonymous_Abstract

    Well-known member
  • Aug 7, 2018
    6,036
    4,566
    113
    Messier 87
    Method Create for Proper Case

    package com.benz.test;

    public class Reverse {

    public static void main(String[] args)
    {
    Reverse rev=new Reverse();
    String nstr = rev.toProper("hEllo WORld beNz hOW are yoU");
    System.out.println(nstr);
    }
    public void reverse(int value)
    {
    int rem;
    while(value>0)
    {
    rem = value % 10;
    System.out.print(rem);
    value = value/10;
    }
    }

    public String toProper(String str)//hEllo WORld
    {
    if(str.trim().length()>0)
    {
    String newstr=null;
    str =str.toLowerCase();//hello world
    String[] st = str.split(" ");//hello , world
    char[] ch=null;
    for(String s : st)
    {
    ch = s.toCharArray(); //'h' 'e' 'l' 'l' 'o' , 'w' 'o' 'r' 'l' 'd'
    ch[0] = Character.toUpperCase(ch[0]);//'H' ,'W'
    if(newstr==null)
    newstr = new String(ch);
    else
    newstr +=" "+ new String(ch);
    }
    return newstr;

    }else
    return str;
    }
    }
     

    Anonymous_Abstract

    Well-known member
  • Aug 7, 2018
    6,036
    4,566
    113
    Messier 87
    Single Linked List In Java
    package com.benz.test;

    import java.util.Scanner;

    class Node{
    int data;
    Node link;

    public Node(int data,Node link)
    {
    this.data=data;
    this.link=link;
    }
    }

    public class SingleLinkedList {

    Node root=null;
    Scanner sc = null;

    public SingleLinkedList()
    {
    sc = new Scanner(System.in);
    }
    public void details()
    {
    while(true)
    {
    System.out.println("1.append");
    System.out.println("2.begin");
    System.out.println("3.after");
    System.out.println("4.delete");
    System.out.println("5.display");
    System.out.println("6.swap");
    System.out.println("7.exit");

    int ch;
    System.out.println("Enter your choice");
    ch = sc.nextInt();

    switch(ch)
    {
    case 1:append();break;
    case 2:begin();break;
    case 3:after();break;
    case 4:delete();break;
    case 5:display();break;
    case 6:swap();break;
    case 7:System.exit(4);
    default:System.out.println("Invalid Input");
    }
    }
    }

    public void append()
    {
    Node temp;
    System.out.println("Enter a Value");
    int value = sc.nextInt();
    temp = new Node(value,null);

    if(root==null)
    {
    root=temp;
    }else
    {
    Node p;
    p=root;
    while(p.link != null)
    {
    p= p.link;
    }
    p.link=temp;
    }
    }
    public void begin()
    {
    Node temp;
    System.out.println("Enter a Value");
    int value = sc.nextInt();
    temp = new Node(value,null);

    if(root==null)
    {
    root=temp;
    }else {
    temp.link=root;
    root =temp;

    }
    }
    public void after()
    {
    int loc;
    System.out.println("Enter the location to add node");
    loc = sc.nextInt();

    if(loc>=length())
    {
    System.out.println("Invalid location");
    System.out.println("Current node is "+length());
    }else {
    Node temp;
    System.out.println("Enter a Value");
    int value = sc.nextInt();
    temp = new Node(value,null);

    Node p=root;

    int i=1;
    while(i<loc)
    {
    p =p.link;
    i++;
    }
    temp.link=p.link;
    p.link=temp;
    }
    }

    public void delete()
    {
    if(root==null) {
    System.out.println("No nodes in the list");
    }else
    {
    int loc;
    System.out.println("Enter the location to delete node");
    loc = sc.nextInt();

    Node temp;

    if(loc>length())
    {
    System.out.println("Invalid location");
    System.out.println("Current node is "+length());
    }else if(loc==1)
    {
    temp =root;
    root = temp.link;
    temp.link=null;
    }else {
    int i=1;
    temp=root;
    Node p;
    while(i<loc-1)
    {
    temp=temp.link;
    i++;
    }
    p = temp.link;
    temp.link = p.link;
    p.link=null;



    }
    }
    }
    public void display()
    {
    Node temp =root;
    if(root==null)
    {
    System.out.println("No nodes in the list");
    }else {
    while(temp != null)
    {
    System.out.print(temp.data+"-->\t");
    temp=temp.link;
    }
    System.out.print("\n");
    }
    }
    public int length()
    {
    int len=0;
    Node temp =root;
    if(temp == null)
    return len;
    else {
    while(temp !=null)
    {
    len +=1;
    temp = temp.link;
    }
    return len;
    }
    }

    public void swap()
    {
    Node temp = root;
    if(root==null)
    {
    System.out.println("No nodes in the list");
    }else {
    int loc;
    System.out.println("Enter the location to swap node");
    loc = sc.nextInt();

    if(loc>=length())
    {
    System.out.println("Invalid location");
    System.out.println("Current node is "+length());
    }else if(loc==1)
    {
    //System.out.println("Invalid Location");
    Node p,q,r;
    p=temp;
    q=p.link;
    r=q.link;

    p.link=r;
    q.link=p;
    root =q;
    }else {
    Node p,q,r;
    p=temp;
    int i=1;
    while(i<loc-1)
    {
    p=p.link;
    i++;
    }
    q =p.link;
    r=q.link;

    q.link= r.link;
    r.link=q;
    p.link = r;
    }
    }
    }
    public static void main(String[] args) {

    SingleLinkedList li =new SingleLinkedList();
    li.details();
    }

    }
     

    Anonymous_Abstract

    Well-known member
  • Aug 7, 2018
    6,036
    4,566
    113
    Messier 87
    Circular Queue in Java

    package com.benz.test;

    import java.util.Scanner;

    public class Cqueue {


    private int size;
    private int rear,front;
    private int[] cqueu=null;
    private boolean exit=false;
    Scanner sc=null;
    public Cqueue(int size,int rear,int front)
    {
    this.size=size;
    this.front=front;
    this.rear=rear;
    cqueu=new int[this.size];
    }
    public void show()
    {
    while(exit==false)
    {
    sc= new Scanner(System.in);
    System.out.println("1. Enqueue");
    System.out.println("2. Dequeue");
    System.out.println("3. Display");
    System.out.println("4. Exit");

    System.out.println("Enter your choice");
    int x = sc.nextInt();

    switch(x)
    {
    case 1:System.out.println("Enter a Value");
    enqueue(sc.nextInt());break;
    case 2:dequeue();break;
    case 3:display();break;
    case 4:exit();break;
    default:System.out.println("Invalid Input");break;
    }
    }
    }
    public void exit()
    {

    System.out.println("Program is Exited");
    exit = true;

    }
    public void enqueue(int value)
    {
    if(rear==size-1 && front==0 || front==rear+1)
    System.out.println("Queue is Overflow");
    else if(rear==-1 && front==-1)
    {
    front=rear=0;
    cqueu[rear]=value;
    System.out.println(value +" is inserted");

    }else if(rear==size-1 && front !=0)
    {
    rear=0;
    cqueu[rear]=value;
    System.out.println(value +" is inserted");
    }else {
    rear++;
    cqueu[rear]=value;
    System.out.println(value +" is inserted");

    }
    }
    public void dequeue()
    {
    if(front==-1 && rear==-1)
    System.out.println("Queue is Empty");
    else if(front==rear)
    {
    int del=cqueu[front];
    System.out.println(del+" is deleted");
    front=rear=-1;
    }else if(front==size-1)
    {
    int del=cqueu[front];
    System.out.println(del+" is deleted");
    front=0;

    }else {
    int del=cqueu[front];
    System.out.println(del+" is deleted");
    front +=1;
    }
    }
    public void display()
    {
    if(rear==-1 && front==-1)
    System.out.println("Queue is Empty");
    else if(rear >=front)
    {
    for(int i=front;i<=rear;i++)
    {
    System.out.print(cqueu+"\t");
    }
    System.out.print("\n");
    }else {
    for(int i=front;i<size;i++)
    {
    System.out.print(cqueu+"\t");
    }

    for(int i=0;i<=rear;i++)
    {
    System.out.print(cqueu+"\t");
    }

    System.out.print("\n");
    }
    }
    public static void main(String[] args)
    {
    Cqueue que= new Cqueue(6,-1,-1);
    que.show();
    }


    }
     

    Anonymous_Abstract

    Well-known member
  • Aug 7, 2018
    6,036
    4,566
    113
    Messier 87
    Reverse a given String in java without StringBuffer/Builder..
    Intern Technical interview @ Virtusa

    public static void main(String[] args) {
    TestDemo td = new TestDemo();

    String str = "Fuck";
    str = td.reverse(str).toString();
    System.out.println(str);

    }

    public StringBuffer reverse(String str) throws ArrayIndexOutOfBoundsException
    {

    char[] ch = str.toCharArray();//'F','u','c','k'
    int len = ch.length;
    //char[] ch2=new char[len];
    StringBuffer sb = new StringBuffer();
    for(int i=len-1;i>=0;i--)
    {
    sb.append(ch);
    }

    return sb;
    }