Zoho Interview Question for SDE1s


Country: United States
Interview Type: Written Test




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

public static void altSorting(ArrayList<Integer> intList) {
		ArrayList<Integer> altIntList = new ArrayList<Integer>();
		Collections.sort(intList);
		int sz = intList.size();
		int lastInx = sz - 1;
		int inx = 0;
		while(inx < (sz/2)) {
			altIntList.add(intList.get(lastInx));
			altIntList.add(intList.get(inx));
			inx++;
			lastInx--;
		}
		if (inx == lastInx ) {
			altIntList.add(intList.get(inx));
		}
		System.out.println(altIntList);
	}

- Anonymous June 23, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

and

public static void main(String[] args) {

int[] myArray = {1, 2, 3, 4, 5, 6, 7};
int[] newArray = new int[myArray.length];

int length = myArray.length-1;

int evenCount = 0;
int oddCount = 1;
for(int i = 0; i < newArray.length; i++) {


if(i % 2 == 0) {
newArray[i] = myArray[length - evenCount];
evenCount++;
}
else {
newArray[i] = myArray[i - oddCount];
oddCount++;
}
}
}

and

- Saurabh June 24, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void main(String[] args) {

int[] myArray = {1, 2, 3, 4, 5, 6, 7};
int[] newArray = new int[myArray.length];

int length = myArray.length-1;

int evenCount = 0;
int oddCount = 1;
for(int i = 0; i < newArray.length; i++) {


if(i % 2 == 0) {
newArray[i] = myArray[length - evenCount];
evenCount++;
}
else {
newArray[i] = myArray[i - oddCount];
oddCount++;
}
}
}

- Saurabh June 24, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void main(String[] args) {

        int[] myArray = {1, 2, 3, 4, 5, 6, 7};
        int[] newArray = new int[myArray.length];

        int length = myArray.length-1;

        int evenCount = 0;
        int oddCount = 1;
        for(int i = 0; i < newArray.length; i++) {


            if(i % 2 == 0) {
                newArray[i] = myArray[length - evenCount];
                evenCount++;
            }
            else {
                newArray[i] = myArray[i - oddCount];
                oddCount++;
            }
        }
    }

- Anonymous June 24, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void main(String[] args) {

        int[] myArray = {1, 2, 3, 4, 5, 6, 7};
        int[] newArray = new int[myArray.length];

        int length = myArray.length-1;

        int evenCount = 0;
        int oddCount = 1;
        for(int i = 0; i < newArray.length; i++) {


            if(i % 2 == 0) {
                newArray[i] = myArray[length - evenCount];
                evenCount++;
            }
            else {
                newArray[i] = myArray[i - oddCount];
                oddCount++;
            }
        }
    }

- Saurabh June 24, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void main(String[] args) {

        int[] myArray = {1, 2, 3, 4, 5, 6, 7};
        int[] newArray = new int[myArray.length];

        int length = myArray.length-1;

        int evenCount = 0;
        int oddCount = 1;
        for(int i = 0; i < newArray.length; i++) {


            if(i % 2 == 0) {
                newArray[i] = myArray[length - evenCount];
                evenCount++;
            }
            else {
                newArray[i] = myArray[i - oddCount];
                oddCount++;
            }
        }
    }

- Saurabh June 24, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

var a = [1,2,3,4,5,6,7].sort(function(a,b){return a < b});

function getNextMaxMin(acc, arr){

    if (arr.length === 1){return acc.push(arr.shift())}
    if (arr.length === 0){return}

    acc.push(arr.shift());
    acc.push(arr.pop());
    return getNext2(acc, arr);

}
var output = [];
getNextMaxMin(output, a);
console.log(output);

- kaleguy June 24, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

var a = [1,2,3,4,5,6,7].sort(function(a,b){return a < b});

function getNextMaxMin(acc, arr){

    if (arr.length === 1){return acc.push(arr.shift())}
    if (arr.length === 0){return}

    acc.push(arr.shift());
    acc.push(arr.pop());
    return getNext2(acc, arr);

}
var output = [];
getNextMaxMin(output, a);
console.log(output);

- kaleguy June 24, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

var a = [1,2,3,4,5,6,7].sort(function(a,b){return a < b});

function getNextMaxMin(acc, arr){

    if (arr.length === 1){return acc.push(arr.shift())}
    if (arr.length === 0){return}

    acc.push(arr.shift());
    acc.push(arr.pop());
    return getNext2(acc, arr);

}
var output = [];
getNextMaxMin(output, a);
console.log(output);

- kaleguy June 24, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Oops.. clicked too many times, and forgot to rename the function. Fixed version below.

var a = [1,2,3,4,5,6,7].sort(function(a,b){return a < b});

function getNextMaxMin(acc, arr){

    if (arr.length === 1){return acc.push(arr.shift())}
    if (arr.length === 0){return}

    acc.push(arr.shift());
    acc.push(arr.pop());
    return getNextMaxMin(acc, arr);

}
var output = [];
getNextMaxMin(output, a);
console.log(output);

- kaleguy June 24, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class AlternativeSorting {

	public static void main(String[] args) {
		ArrayList<Integer> list = new ArrayList(Arrays.asList(1,2,3,7,5,6,4,8));
		Collections.sort(list,Collections.reverseOrder());
		int i=1;
		while(i<list.size()){
			int val = list.remove(list.size()-1);
			list.add(i, val);
			i=i+2;
		}
		System.out.println(list);		
	}
}

- Anonymous June 24, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class AlternativeSorting {

	public static void main(String[] args) {
		ArrayList<Integer> list = new ArrayList(Arrays.asList(1,2,3,7,5,6,4,8));
		Collections.sort(list,Collections.reverseOrder());
		int i=1;
		while(i<list.size()){
			int val = list.remove(list.size()-1);
			list.add(i, val);
			i=i+2;
		}
		System.out.println(list);
		
		
	}
}

- sun99 June 24, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.*;
public class AltSort {
public static void main(String[] args){

int intArr[] = {1,2,3,4,5,6,7};
int lent = intArr.length;
int mp = lent/2;
System.out.println("value of mp" + mp);
int i=lent-1;
System.out.println("value of i" + i);
int j=-1;
while (i>=mp)
{
System.out.println(intArr[i]);
j++;
if (j<mp)
{
System.out.println(intArr[j]);

};
i--;
};

}

}

- Anonymous June 24, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.*;
public class AltSort {
public static void main(String[] args){
		
	int intArr[] = {1,2,3,4,5,6,7};
	int lent = intArr.length;
	int mp = lent/2;
	System.out.println("value of mp" + mp);
	int i=lent-1;
	System.out.println("value of i" + i);
	int j=-1;
	 while (i>=mp)
	 {
		 System.out.println(intArr[i]);
		j++;
		 if (j<mp)
		 {
			 System.out.println(intArr[j]);
		 
		 };
		 i--;
	 };
		 
}

}

- muralidhar78 June 24, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Using C:
It sorts the user provided array first and then just alternately add the elements in the array.

#include <stdio.h>

void sort(int arr[],int left,int right)
{

	int i= left, j= right;
	int pivot = arr[((left+right)/2)];
	int tmp=0;


	/* partition */
	
	while(i<=j)
	{
		while(arr[i]<pivot)
			i++;

		while(arr[j]>pivot)
			j--;
			
		if(i<=j)
		{
			tmp = arr[i];
            arr[i] = arr[j];
            arr[j] = tmp;
            i++;
            j--;
		}


	};

	/* recursion */

	if(left<j)
		sort(arr,left,j);
	if(i<right)
		sort(arr,i,right);
		
} 
int main()
{
	int i;
	int arr[7] = {1,2,3,4,5,6,7};int max=0,min=0;
	int result[7];
	int len = (sizeof(arr)/sizeof(arr[0]));
	//printf("%d\n",arr[0]);
	sort(arr,0,len-1);
	//printf("%d\n",arr[0]);
	for(i=0;i<len;i++)
	{
		if(i%2==0)
		{
		result[i] = arr[(len-1-max)];
		max++;
		}
		else
		{
		result[i] = arr[min];
		min++;
		}
		printf("%d\n",result[i]);	
	}

	return 0;

}

- Denis June 24, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

from collections import deque


def MaxMin(alist):
    d = deque(sorted(alist))
    newList = []
    while len(d) > 0:
        newList.append(d.pop())
        newList.append(d.popleft())
        
    return newList

list1 = [100,21,44,20,24,1]
list1_sort = MaxMin(list1)
print "Original list ", list1
print "SortedList " , list1_sort
    
        
list2 = [100,21,44,20,24,1,5,6,23,52,99,34]
list2_sort = MaxMin(list2)
print "Original list ", list2
print "SortedList " , list2_sort

- Dennis Domingo June 28, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

from collections import deque


def MaxMin(alist):
    d = deque(sorted(alist))
    newList = []
    while len(d) > 0:
        newList.append(d.pop())
        newList.append(d.popleft())
        
    return newList

list1 = [100,21,44,20,24,1]
list1_sort = MaxMin(list1)
print "Original list ", list1
print "SortedList " , list1_sort
    
        
list2 = [100,21,44,20,24,1,5,6,23,52,99,34]
list2_sort = MaxMin(list2)
print "Original list ", list2
print "SortedList " , list2_sort

- Dennis Domingo June 28, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

from collections import deque


def MaxMin(alist):
    d = deque(sorted(alist))
    newList = []
    while len(d) > 0:
        newList.append(d.pop())
        newList.append(d.popleft())
        
    return newList

list1 = [100,21,44,20,24,1]
list1_sort = MaxMin(list1)
print "Original list ", list1
print "SortedList " , list1_sort
    
        
list2 = [100,21,44,20,24,1,5,6,23,52,99,34]
list2_sort = MaxMin(list2)
print "Original list ", list2
print "SortedList " , list2_sort

- ddomingo June 28, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public int[] sort(int[] nums){
		Arrays.sort(nums);
		int[] result = new int[nums.length];
		int start = 0, end = nums.length - 1;
		int index = 0;
		while( index < nums.length ){
			result[index] = nums[end];
			end--;
			index++;
			if( start < end)  result[index] = nums[start];
			index++;
			start++;
		}
		
		return result;
	}

- MyFavCat June 28, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public int[] sort(int[] nums){
		Arrays.sort(nums);
		int[] result = new int[nums.length];
		int start = 0, end = nums.length - 1;
		int index = 0;
		while( index < nums.length ){
			result[index] = nums[end];
			end--;
			index++;
			if( start < end)  result[index] = nums[start];
			index++;
			start++;
		}
		
		return result;
	}

- MyFavCat June 28, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Is there a way to do this without using O(n) additional memory, like all the solution so far?

- Anonymous June 28, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class FirstMaxFirstMin {

    public static void main(String[] args) {

        int[] inputArray1 = {1, 2, 3, 4, 5, 6, 7};
        populateMaxMinCombination(inputArray1);

        int[] inputArray2 = {1, 3, 2, 7, 6, 8, 5, 9, 3};
        System.out.println();
        populateMaxMinCombination(inputArray2);
    }

    private static void populateMaxMinCombination(int[] inputArray) {
        for (int row = 0; row < inputArray.length; row += 2) {
            int min = inputArray[row];
            int max = inputArray[row];
            int minIndex = row;
            int maxIndex = row;
            for (int index = row; index < inputArray.length; index++) {
                if (min > inputArray[index]) {
                    min = inputArray[index];
                    minIndex = index;
                } else if (max < inputArray[index]) {
                    max = inputArray[index];
                    maxIndex = index;
                }
            }

            if (maxIndex != minIndex) {
                if (row == minIndex) {
                    inputArray[maxIndex] = inputArray[row + 1];
                    inputArray[row + 1] = min;
                    inputArray[row] = max;

                } else {
                    inputArray[maxIndex] = inputArray[row];
                    inputArray[row] = max;

                    inputArray[minIndex] = inputArray[row + 1];
                    inputArray[row + 1] = min;
                }
            }

        }

        print(inputArray);
    }

    private static void print(int[] inputArray) {
        for (int index = 0; index < inputArray.length; index++) {
            System.out.print(inputArray[index] + " ");
        }
    }
}

- imarya July 02, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<iostream>
using namespace std;
int a1[10],b1[10];
int sort(int a12[],int n){
for(int i=0;i<n-1;i++){
for(int j=i+1;j<n;j++){
if(a12[i]>a12[j]){
int temp=a12[i];
a12[i]=a12[j];
a12[j]=temp;
}
}
}
}
int main(){
int n;
cout<<"n";
cin>>n;
for(int i=0;i<n;i++){
cin>>a1[i];
}
sort(a1,n);
int i=0,j=n-1,k=0;
while(i<n){
if(i%2==0){
b1[i]=a1[j];
j--;
}
else{
b1[i]=a1[k];
k++;
}
i++;

}
for(int i=0;i<n;i++){
cout<<b1[i];
}
}

- Rohan July 31, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
  int num;
  cin>>num;
  vector<int> a;
 for(int i=0;i<num;i++){
     int x;
     cin>>x;
     a.push_back(x);
    }
    
    sort(a.begin(),a.end());
    
    int maxy,miny,dis_2,dis_1;
    for(int i=0;i<num-1;i=i+2){

maxy = *max_element(a.begin()+i,a.end());
dis_1 = distance(a.begin(),max_element(a.begin()+i,a.end()));
int temp_1 = a[i];
a[i]=maxy;
a[dis_1]=temp_1;

miny = *min_element(a.begin()+i,a.end());
dis_2 = distance(a.begin(),min_element(a.begin()+i,a.end()));
int perm = a[i+1];
a[i+1]=miny;
a[dis_2]=perm;

} 

for(auto i=a.begin();i!=a.end();i++){
    cout<<*i<<" ";
}
    
   
    
   return 0;
}

- Dheeraj N July 13, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
int num;
cin>>num;
vector<int> a;
for(int i=0;i<num;i++){
int x;
cin>>x;
a.push_back(x);
}

sort(a.begin(),a.end());

int maxy,miny,dis_2,dis_1;
for(int i=0;i<a.size()-1;i=i+2){

maxy = *max_element(a.begin()+i,a.end());
dis_1 = distance(a.begin(),max_element(a.begin()+i,a.end()));
int temp_1 = a[i];
a[i]=maxy;
a[dis_1]=temp_1;

miny = *min_element(a.begin()+i,a.end());
dis_2 = distance(a.begin(),min_element(a.begin()+i,a.end()));
int perm = a[i+1];
a[i+1]=miny;
a[dis_2]=perm;

}

for(auto i=a.begin();i!=a.end();i++){
cout<<*i<<" ";
}



return 0;
}

- Dheeraj N July 13, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
int num;
cin>>num;
vector<int> a;
for(int i=0;i<num;i++){
int x;
cin>>x;
a.push_back(x);
}

sort(a.begin(),a.end());

int maxy,miny,dis_2,dis_1;
for(int i=0;i<a.size()-1;i=i+2){

maxy = *max_element(a.begin()+i,a.end());
dis_1 = distance(a.begin(),max_element(a.begin()+i,a.end()));
int temp_1 = a[i];
a[i]=maxy;
a[dis_1]=temp_1;

miny = *min_element(a.begin()+i,a.end());
dis_2 = distance(a.begin(),min_element(a.begin()+i,a.end()));
int perm = a[i+1];
a[i+1]=miny;
a[dis_2]=perm;

}

for(auto i=a.begin();i!=a.end();i++){
cout<<*i<<" ";
}



return 0;
}

- Dheeraj N July 13, 2017 | 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