Interview Question


Country: India
Interview Type: Written Test




Comment hidden because of low score. Click to expand.
1
of 1 vote

int[] yourFun(int[] a1,int[] a2) {
        int length = a1.length + a2.length;
        int[] a3 = new int[length];
        int i = 0;
        for (i = 0; i < a1.length; i++) {
            a3[i] = a1[i];
        }
        for (int j = 0; i < length; i++, j++) {
            a3[i] = a2[j];
        }
        Arrays.sort(a3);
        return a3;
    }

- kishore August 04, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

please write a code for this one in java..

- Muni June 29, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class Mergetwoarrays {

public static void main (String[] args)
{
int a[] = {1,3,5,7};
int b[] = {3,6,8,9};
int q=0;
int p=0;
int c=0;
int r= a.length;
int s = b.length;
int num=r+s;
int k[] = new int[num];
while (p < r && q < s)
{
if(a[p]<=b[q])
{
k[c++]= a[p++];
}else
{
k[c++] = b[q++];
}
}
while(p < r)
// Copy rest of first
{ k[c++] = a[p++];
}
while(q < s) // Copy rest of right
{
k[c++] = b[q++];
}
for (int m=0; m<k.length; m++)
{
System.out.println(k[m]);
}
}
}

- rodik June 29, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.Arrays;


public class MergeAndSort {
public static int[] sortedMerge(int[] a1, int[] a2){
int[] a3 = new int[a1.length + a2.length];
System.arraycopy(a1, 0, a3, 0, a1.length);
System.arraycopy(a2, 0, a3, a1.length, a2.length);
int temp = 0;
for(int i = 1; i < a3.length-1; i++){
for(int j = i+1; j < a3.length; j++){
if(a3[i] > a3[j]){
temp = a3[i];
a3[i] = a3[j];
a3[j] = temp;
}
}
}
return a3;
}
public static void main(String[] args){
int[] a1={1,3,5,7};
int[] a2={3,6,8,9};
int[] a3 = sortedMerge(a1, a2);
System.out.println(Arrays.toString(a3));
}
}

- Deepthi June 30, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static int[] mergeArray(int[] a1, int[] a2){
		if((a1 == null || a1.length<1) && (a2 == null || a2.length < 1))
			return null;
		if(a1 == null || a1.length < 1){
			return a2;
		}
		if(a2 == null || a2.length < 1){
			return a1;
		}
		
		int[] a3 = new int[a1.length + a2.length];
		int i = 0, j = 0, k = 0;
		while(i < a1.length){
			while(j<a2.length){
				if( i >= a1.length || j >= a2.length) break;
				
				if(a1[i] <= a2[j]){
					a3[k] = a1[i];
					k++;
					i++;
				}  else {
					a3[k] = a2[j];
					k++;
					j++;
				}
			}
		}
		
		if(i<a1.length){
			for(; i < a1.length; i++) {
				a3[k] = a1[i];
				k++;
			}
		}
		if(j<a2.length){
			for(;j<a2.length;j++) {
				a3[k] = a2[j];
				k++;
			}
		}
		
		return a3;
	}

- Ranjith July 03, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.*;
public class arrr {

public static void main(String[] args) {

int[] a1={1,3,5,7};
int[] a2={3,6,8,9};

ArrayList<Integer> al=new ArrayList<Integer>();

for (int i = 0; i < a1.length; i++) {

al.add(a1[i]);

}

for (int i = 0; i < a2.length; i++) {

al.add(a2[i]);
}
Object o[]=new Object[al.size()];

o=al.toArray();

Arrays.sort(o);

int a3[]=new int[o.length];

for (int i = 0; i < o.length; i++) {

a3[i]=(int)o[i];

}
for (int i = 0; i < a3.length; i++) {
System.out.print(a3[i]);
}
}
}

- p.vamsikrishna February 13, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class Test {
	private static int[] mergedArr;
	
	public static void main(String[] args) {
		int[] arr1 = {1,3,5,7};
		int[] arr2 = {3,6,8,9};
		
		mergedArr = new int[arr1.length + arr2.length];
		mergeArray(arr1, arr2);
		
		for(int i : mergedArr) {
			System.out.println(i);
		}
	}
	
	public static void mergeArray(int[] left, int[] right) {
		int i=0;
		int j=0;
		int k=0;
		
		while(i < left.length && j < right.length) 
		{
			if(left[i] < right[j]) 
			{
				mergedArr[k++] = left[i++];
			}
			else
			{
				mergedArr[k++] = right[j++];
			}
		}
		
		while(i < left.length) {
			mergedArr[k++] = left[i++];
		}
		
		while(j < right.length) {
			mergedArr[k++] = right[j++];
		}
	}
}

- anom June 07, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

using System;
class Program
{
static void Main(string[] args)
{
int[] a1 = { 1,2,3,4};
int[] a2 = (int[]) a1.Clone();
a2[1] = 5;
a2[2] = 6;
int[] a3 = new int[4];
Array.Copy(a2, a1, 3);
Console.WriteLine(a1[1]);
Console.Read();
}
}

- Anonymous June 21, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

public int[] merge(int a1[], int a2[]) {
        int[] a3 = new int[a1.length + a2.length];

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

        while (i < a1.length && j < a2.length) {
            if (a1[i] < a2[j]) {
                a3[k] = a1[i];
                i++;
            } else {
                a3[k] = a2[j];
                j++;
            }
            k++;
        }
        while (i < a1.length) {
            a3[k] = a1[i];
            k++;
            i++;
        }
        while (j < a2.length) {
            a3[k] = a2[j];
            k++;
            j++;
        }

        return a3;
    }

- Jas June 29, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

i think first while loop is not doing right job

it should be like

while(i < a1.len && j < a2.len)
{
     if(a1[i] < a2[j]) 
    {
       a3[k] = a1[i];
       i++;
    }
   else if(a1[i] > a2[j])
   {
       a3[k] = a2[j];
       j++;
   }
  else
  {
      a3[k] = a1[i];
      i++;
      j++;
  }
  k++;
}

- Kavita June 30, 2014 | Flag


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More