Persistent Systems Interview Question for Software Engineer / Developers


Country: India
Interview Type: Written Test




Comment hidden because of low score. Click to expand.
2
of 4 vote

public void sortArray(int[] array)
	{
		int left = 0;
		int right = array.length - 1;
		
		while(left < right)
		{
			while(array[left] == 0)
				left++;
			while(array[right] == 1)
				right--;
			
			array[left] = 0;
			array[right] = 1;
		}
	}

- Dev February 20, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

check this for the test case
i/p : 011111
1) left = 0 , right =5
2) enters while loop
{
left ++ ; (left=1)
right--; (right=4)

arr[left] = 0; // this statement gives wrong result

}

check this if i am wrong.....



}

- pankaj March 01, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

a small correction will work out

if(arr[left] > a[right])
{
arr[left]=0;
arr[right]=1;
}

- pankaj March 01, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

apparently it's not correct

- Ted May 17, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Above Solution is wrong,

public static void sortZeroOne(int[] data) {
		if (data == null || data.length==1){
			return;
		} 
		int left = 0;
		int right = data.length-1;
		while (left<right) {
			while (data[left]==0){
				left++;
			} 
			while (data[right]==1){
				right--;
			} 
			if (left < right) {
				data[left]=0; 
				data[right]=1; 
				left++; 
				right--;
			}
		}
	}

- Adnan Ahmad September 22, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

want to correct a mistake in the above qstn, 0's towards left and 1's towards right..

- sumit February 19, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Please check this code...

#include<stdio.h>
main(){
 int a[] = {0,1,0,1,0,1,0,1,0,1};
 int i=0,j,size=0;
 size = sizeof(a)/sizeof(int);
 for(j=1;j<size;j++){
   if(a[i] == 1 && a[j] == 0){
     a[i] = 0;
     a[j] = 1;
   }
   if(a[i] == 1 && a[j] == 1)
     continue;
   i++;  
 }
 for(j=0;j<size;j++)
   printf("%d",a[j]);
 printf("\n");

}

- Sravan June 27, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

//cur initialised to starting of array
//n is the no. of elements
//initial state is set to 0 and signifies whether till now any 1 is encountered or not
//once state is changed to 1 and we get any 0 then we bubble it

cur=0;
while(cur<n)
{
if((state==0)&&(a[cur]==1))
state=1;
else if((state==1)&&(a[cur]==0))
{
for(i=cur;(i>=0)&&(a[i-1]==1);i--)
a[i-1]^=a[i]^=a[i-1]=a[i-1]^a[i]; //swapping the elements
}
cur++;
}

- andy_kumar February 19, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

// Since the array consists of only 0's and 1's,
1. Initialize two pointers: j = 0, k = array.length - 1;
2. while(j < k) {
if (a[k] < a[j] { // if '0' < '1', swap, for automatic sorting
a[k] = 1;
a[j] = 0;
k--; // point to next swap candidate from right ('0')
}
// if a[k] = '1', a[k] is a swap candidate, if no swap is done for this a[k], we're done with sorting entire array
// If a[j) is 0, it's sorted already, just point to next swap candidate from left ('1')
j++;
}
}

- taojfew February 19, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

well this can be done in O(len).
following is my implementation

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int main()
{
int T;scanf("%d",&T);
while(T--) {
char arr[1000];
scanf("%s",arr);
int len=strlen(arr);int one,zero;
for(int j=0; j< len;j++) {

if(arr[j]=='0') zero++;
if(arr[j] == '1') one++;
}
int p1=0,p2=len-1,tmp;tmp=len;
while(1) {
for(int j=p1;j<len;j++) if(arr[j] == '1') {p1=j;break;} else zero--;
arr[p1]='0';zero--;
for(int j=p2;j>=0;j--) if(arr[j]=='0') {p2=j;break;} else one--;
arr[p2]='1';one--;
if(one <=0 || zero <=0) break;
}
printf("%s",arr);

}
return 0;
}

- solve February 19, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

well this can be done in O(len).
following is my implementation

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int main()
{
int T;scanf("%d",&T);
while(T--) {
char arr[1000];
scanf("%s",arr);
int len=strlen(arr);int one,zero;
for(int j=0; j< len;j++) {

if(arr[j]=='0') zero++;
if(arr[j] == '1') one++;
}
int p1=0,p2=len-1,tmp;tmp=len;
while(1) {
for(int j=p1;j<len;j++) if(arr[j] == '1') {p1=j;break;} else zero--;
arr[p1]='0';zero--;
for(int j=p2;j>=0;j--) if(arr[j]=='0') {p2=j;break;} else one--;
arr[p2]='1';one--;
if(one <=0 || zero <=0) break;
}
printf("%s",arr);

}
return 0;
}

- solve February 19, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

for(i=0;i<n;i++)
{
if(a[i]==1)
{
for(j=i;j<n;j++)
{
if(arr[j]==0)
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
break;
}
}
}
}

- venkatesh February 19, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

in place merge sort

- ravi February 19, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 2 vote

int k=arr.size(),int one=0,int zero=0;
for(int i=0;i<k;i++)
{
if(arr[i]) one++;
else zero++;
}
i=0;
while(zero){
arr[i]=0;
i++;
zero--;
}
while(one)
{
arr[i]=1;
i++;
one--;
}

and that's it. it's a basic counting sort.

- radu.cruceru February 19, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

static String sortZerosOnesString(String input){
		char[]a=input.toCharArray();

		final char zero='0';
		final char one='1';
		
		int i=0;
		int j=a.length-1;
		while(i<j){
			while(a[i]==zero)
				i++;
			while(a[j]==one)
				j--;
			
			if(i<j){
				char temp=a[i];
				a[i]=a[j];
				a[j]=temp;
			}
			
		}
		
		return new String(a);
	}

- Mau February 19, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

{

#include"conio.h"
#include"iostream"
using namespace std;

int main()
{
int a[15]= {0,1,0,0,1,0,1,1,1,0,1};
int i,j=0;
int temp;
for(i=0;i<11;i++)
{
if(a[i]>a[i+1])
{
temp=a[i];
a[i]=a[i+1];
a[i+1]=temp;
i=0;

}
}
getch();
}




}

- prashantsitm February 20, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include"conio.h"
#include"iostream"
using namespace std;

int main()
{
int a[15]= {0,1,0,0,1,0,1,1,1,0,1};
int i,j=0;
int temp;
for(i=0;i<11;i++)
{
if(a[i]>a[i+1])
{
temp=a[i];
a[i]=a[i+1];
a[i+1]=temp;
i=0;

}
}
for(i=0;i<11;i++)
cout<<a[i];
getch();
}

- prashantsitm February 20, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Wrong Solution... You are passing 5 0's. while printing the array after processing.. its printing 6 0's.

Check you answer before posting it, why you are wasting others time.

- Devesh March 06, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Wrong Solution... You are passing 5 0's. while printing the array after processing.. its printing 6 0's. 

Check you answer before posting it, why you are wasting others time.

- Devesh March 06, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

take to pointers starting one from the beginning and one from the end and traverse them towards the center of the array .while doing that compare and swap..u can finish in o(n)

- shivacherukuri February 20, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void sortArray (int arr[], int size){

	int i=0, j=size-1;	

	while(i<j){
		
		if(arr[i] == 1 and arr[j]== 0)
			swap(arr,i,j); 
			i++; j--;
		else if(arr[i] == 0)
			i++;
		else if(arr[j] == 1)
			j--;
	}

}

- abhijeet15oct February 28, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<iostream>
using namespace std;

void inplace( int *A , int size )
{
//00010111
int l = 0  ,h=size;

while( l < h )
{

if( A[l] == 0 )
l++;

if( A[h] == 1 )
h--;

if( A[l]== 1 && A[h] == 0  )
{
        A[l] = A[l] ^ A[h];
        A[h] = A[l] ^ A[h];
        A[l] = A[l] ^ A[h];
l++;
h--;
}
}


for( int i=0; i < size ; i++ )
{
        cout << A[i];

}
}


int main()
{

        int Ar[] = {0,1,1,1,1,0,0,0,0,1};
        inplace( Ar , 10 );
return 0;
}

- Anonymous March 09, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int left =0;
int right= array.lenth();
while(left<right){
if(a[left]>a[right]){
swap(a[left],a[right]);
left++;
right--;
}
else if((a[left]==a[right]) && a[left]==0){
left++;
}else if((a[left]==a[right]) && a[left]==1){
right--;
}else{
left++;
right--;
}
}

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

I was told to sort it..using concept of binary numbers..

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

Assuming that the integer array only contains zeros and ones here is another solution

private static void sort1(int[] a) {
		int zerosCount = 0;
		for(int i = 0; i<a.length ; i++) {
			if(a[i] == 0)
				zerosCount++;
		}

		for(int j=0 ; j<a.length ; j++) {
			if(j < zerosCount)
				a[j] = 0;
			else
				a[j] = 1;
		}

}

- sunand March 16, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public void sortArray(int[] array)
{
int left = 0;
int right = array.length - 1;

while(left < right)
{
while(array[left] != 1)
left++;
while(array[right] != 0)
right--;
if(left<right){
array[left] = 0;
array[right] = 1;
}
}
}

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

public static void sortArray(int[] array)
	{
		int left = 0;
		int right = array.length - 1;
		
		while(left < right)
		{
			while(array[left] != 1)
				left++;
			while(array[right] != 0)
				right--;
			if(left<right){
			array[left] = 0;
			array[right] = 1;
			}
		}
	}

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

public void sortArray(int[] array)
{
int left = 0;
int right = array.length - 1;

while(left < right)
{
while(array[left] != 1)
left++;
while(array[right] != 0)
right--;
if(left<right){
array[left] = 0;
array[right] = 1;
}
}
}

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

public static void sortArray(int[] array)
	{
		int left = 0;
		int right = array.length - 1;
		
		while(left < right)
		{
			while(array[left] != 1)
				left++;
			while(array[right] != 0)
				right--;
			if(left<right){
			array[left] = 0;
			array[right] = 1;
			}
		}
	}

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

int[] arr = {0,0,1,0,1,1,0,1,1,1,0,1,0,0,0,1};

System.out.println("Orginal Array : ");
for(int i=0;i<arr.length;i++){
System.out.print(arr[i]+", ");
}

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

for(int j=i;j<arr.length;j++){

if(arr[i]> arr[j]){

int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}


}

System.out.println();
System.out.println("Sorted Array : ");
for(int i=0;i<arr.length;i++){
System.out.print(arr[i]+", ");
}
}

- sambhu April 08, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int[] arr = {0,0,1,0,1,1,0,1,1,1,0,1,0,0,0,1};

System.out.println("Orginal Array : ");
for(int i=0;i<arr.length;i++){
System.out.print(arr[i]+", ");
}

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

for(int j=i;j<arr.length;j++){

if(arr[i]> arr[j]){

int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}


}

System.out.println();
System.out.println("Sorted Array : ");
for(int i=0;i<arr.length;i++){
System.out.print(arr[i]+", ");
}
}

- sambhu April 08, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

just count zerros number, ones number, flash buffer and write over with right order O(N)

- Zack October 14, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

while(left < right)
{
while(array[left] == 0)
left++;
array[left]=0;
while(array[right] == 1)
right--;
array[right]=1;

}

- This will work April 29, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

while(left < right)
{
while(array[left] == 0)
left++;
array[left]=0;
while(array[right] == 1)
right--;
array[right]=1;
}

- This will work April 29, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

def movezeros(l):
j=-1
for i in range(len(l)):
if i==0:
j+=1
l[i],l[j]=l[j],l[i]
return l

- Anonymous September 11, 2020 | 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