Interview Question for Java Developers


Country: India
Interview Type: Written Test




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

public class Question1 {
public static void main(String[] args) {
int inarr[]={7,4,2,5,1,9,6};
int outarr[]=new int[inarr.length];
// sort the Array
sotr(inarr);
for (int i = 0; i < inarr.length; i++) {
System.out.print(inarr[i]);
}
int k=outarr.length;
for (int i = 0,j=0; i < inarr.length; i++) {
if(i%2==0){
outarr[j]=inarr[i];
j++;
}else{
outarr[--k]=inarr[i];

}
}

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

private static void sotr(int[] arr) {
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr.length; j++) {
if(arr[i]<arr[j]){
int temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}

}

}

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

int[] arr = new int[] { 1, 4, 5, 2, 6, 7, 3, 8 };
            int max = arr.Max();
            Func<int, int> separate_odds_and_evens = delegate(int num)
                {
                    if (num % 2 == 0) return max - num;
                    return num - max;
                };
            var sibla = arr.OrderBy(separate_odds_and_evens).ToArray();

- Dr.Sai October 16, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

I dont think its a separation of Odd and Even numbers.
Its two consecutive numbers which are placed at either end of the array.
The logic will be:
int [] input = {7,4,2,5,1,9,6};
int length = input.length;
int start = 0;
int[] sortedInput = quicksort(input);
int[] solution = new int[length];
for(int i=1;i<length-1;i++ ){
while(start<=length-1/2 && length > =length/2){
if(i == length-1){
solution[start] = sortedInput[i-1];
solution[length] = sorttedInput[i];
break;
}else{
solution[start++] = sortedInput[i-1];
solution[length--] = sortedInput[i];
}
}
}

public void quicksort(int[] array){
int length = array.length;
qSort(0,length-1);
}
public void quicksort(int lower, int higher){
int divider = array[(lower + (higher-lower)/2)];
int i = lower;
int j = higher;
while(i <= j ){
while(array[i] < divider){
i++;
}

while(array[j] > divider){
j--;
}
if(i<=j){
swap(i,j);
i++;j--;
}
}
if(lower < j){ qSort(lower,j); }
if(i < higher){ qSort(i,higher);}

}

- Anun October 16, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

int[] print(int[] array)
{
Array.sort(array);
for(int i = 1;i<array.length-1;i++)
{
for(int j=i ;i<array.length-1;j = j+2)
swap(array, i, i+1);
}
return array;
}

- Chang October 17, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

You need to sort the array . Once sorted you could shuffle it as follows :

shuffle :: [Int] -> [Int]
shuffle (x:[]) = [x]
shuffle (x:y:xs) = [x] ++ shuffle(xs) ++ [y]



shuffle [1,2,4,5,6,7,9]
[1,4,6,9,7,5,2]

- Anonymous October 21, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

You can get away from sorting the array and the following approach would directly populate the array in place.

shuffArray(int[] arr){
		int low = 0;
		int high = arr.length;
		int minPos;

		while(low < high){
			minPos = findMinPos(arr, low, high);
			swap(arr[low], arr[minPos]);
			low++;
			minPos = findMinPos(arr, low, high);
			swap(arr[high], arr[minPos]);
			high--;
		}
	}

- Hitman October 22, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class Question1 {
public static void main(String[] args) {
int inarr[]={7,4,2,5,1,9,6};
int outarr[]=new int[inarr.length];
// sort the Array
sotr(inarr);
for (int i = 0; i < inarr.length; i++) {
System.out.print(inarr[i]);
}
int k=outarr.length;
for (int i = 0,j=0; i < inarr.length; i++) {
if(i%2==0){
outarr[j]=inarr[i];
j++;
}else{
outarr[--k]=inarr[i];

}
}

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

private static void sotr(int[] arr) {
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr.length; j++) {
if(arr[i]<arr[j]){
int temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}

}

}

- Banwari Sharma October 29, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

private static int[] reorderArray(int[] arr){
        Arrays.sort(arr);
        int[] result = new int[arr.length];
        for (int i = 0 ; i <= arr.length/2; i++){
            result[i] = arr[i*2];
            if (i != arr.length/2) {
                result[arr.length - 1 - i] = arr[i * 2 + 1];
            }
        }
        return result;
    }

- GK February 01, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

/*arr :[1, 2, 4, 5, 6, 7, 9]
*/
int arr[]={7,4,2,5,1,9,6};
int out[]=new int[arr.length];
Arrays.sort(arr);
System.out.println(Arrays.toString(arr));
for (i=0;i < arr.length;i++) {
if(i%2==0) {
out[k]=arr[i];
k++;
}
int leng=(arr.length/2)+1;
for (int i = arr.length; i > 0; i=i-2) {
if(leng!=arr.length)
out[leng++]=arr[i-2];

}
}
System.out.print(Arrays.toString(out));
/*out :[1, 4, 6, 9, 7, 5, 2]*/

- Shoeb Siddiqui March 01, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

/*arr :[1, 2, 4, 5, 6, 7, 9]
*/int arr[]={7,4,2,5,1,9,6};
int out[]=new int[arr.length];
Arrays.sort(arr);
System.out.println(Arrays.toString(arr));
for (i=0;i < arr.length;i++) {
if(i%2==0) {
out[k]=arr[i];
k++;
}int leng=(arr.length/2)+1;
for (int i = arr.length; i > 0; i=i-2) {
if(leng!=arr.length)
out[leng++]=arr[i-2];
}}
System.out.print(Arrays.toString(out));
/*out :[1, 4, 6, 9, 7, 5, 2]*/

- Shoeb Siddiqui March 01, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

/*arr :[1, 2, 4, 5, 6, 7, 9]
*/int arr[]={7,4,2,5,1,9,6};int out[]=new int[arr.length];
Arrays.sort(arr);
System.out.println(Arrays.toString(arr));
for (i=0;i < arr.length;i++) {
if(i%2==0) {
out[k]=arr[i];
k++; }
int leng=(arr.length/2)+1;
for (int i = arr.length; i > 0; i=i-2) {
if(leng!=arr.length)
out[leng++]=arr[i-2];
}}System.out.print(Arrays.toString(out)); /*out :[1, 4, 6, 9, 7, 5, 2]*/

- Shoeb Siddiqui March 01, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class Test_7 {
static int k=0,i=0;
public static void main(String[] args) {
/*arr :[1, 2, 4, 5, 6, 7, 9]
*/
int arr[]={7,4,2,5,1,9,6};
int out[]=new int[arr.length];
Arrays.sort(arr);
System.out.println(Arrays.toString(arr));
for (i=0;i < arr.length;i++) {
if(i%2==0) {
out[k]=arr[i];
k++;
}
int leng=(arr.length/2)+1;
for (int i = arr.length; i > 0; i=i-2) {
if(leng!=arr.length)
out[leng++]=arr[i-2];

}
}
System.out.print(Arrays.toString(out));
/*out :[1, 4, 6, 9, 7, 5, 2]*/
}
}

- Shoeb Siddiqui March 01, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class Test_7 {
static int k=0,i=0;
public static void main(String[] args) {
/*arr :[1, 2, 4, 5, 6, 7, 9]
*/int arr[]={7,4,2,5,1,9,6};
int out[]=new int[arr.length];
Arrays.sort(arr);
System.out.println(Arrays.toString(arr));
for (i=0;i < arr.length;i++) {
if(i%2==0) {
out[k]=arr[i];
k++; }
int leng=(arr.length/2)+1;
for (int i = arr.length; i > 0; i=i-2) {
if(leng!=arr.length)
out[leng++]=arr[i-2]; }}
System.out.print(Arrays.toString(out));
/*out :[1, 4, 6, 9, 7, 5, 2]*/ }}

- Shoeb Siddiqui March 01, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class Test_7 {
static int k=0,i=0;
public static void main(String[] args) {
/*arr :[1, 2, 4, 5, 6, 7, 9] */
int arr[]={7,4,2,5,1,9,6};
int out[]=new int[arr.length];
Arrays.sort(arr);
for (i=0;i < arr.length;i++) {
if(i%2==0) {
out[k]=arr[i];
k++;
}
int leng=(arr.length/2)+1;
for (int i = arr.length; i > 0; i=i-2) {
if(leng!=arr.length)
out[leng++]=arr[i-2];
}}
System.out.print(Arrays.toString(out));
/*out :[1, 4, 6, 9, 7, 5, 2]*/
}
}

- Shoeb Siddiqui March 01, 2015 | Flag Reply


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