Adobe Interview Question for Software Engineer in Tests


Country: India
Interview Type: In-Person




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

void sort(int * a,int len)
{
int i= 0, j = len;
while(j>i){
if(a[i] == 0){
i++;
continue;
}
while((a[--j] == 1)&&j>=0);
if(j>i)
SWAP(a[i],a[j]);
}
}

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

Actually, the interviewer first asked that the values in the array lie between 0 and 100. How can I use this information while sorting. After that he re-formatted the array to contain only 0's and 1's.

- Mike February 13, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

If array lie between 0 to100 then i think you have to use extra space which will keep the count of each digit of the array, than accordingly sort the array

- Ali_BABA February 13, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

If the range is 1..100 then count in phase one, and the rewrite (fill) the array with the numbers - O(N) time, O(1) space
Naturally this does not work if the numbers are just keys, in that case count sorting - which is pretty much the same - works.

- Selmeczy, Péter February 13, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Ooooops, one more thing: while(a[--j] == 1);
nicely decrementing j below 0...
Should add j>=0 &&

- Selmeczy, Péter February 14, 2012 | Flag
Comment hidden because of low score. Click to expand.
2
of 4 vote

Go Through the array and count number of zeros(say it countZeros). Now go through again and set it to zero till index countZeros-1. remaining array should be 1.
TC O(n)
space complexity : constant

- loveCoding February 13, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
2
of 2 vote

/* Returns 1 if successful, 0 otherwise */
int sort01( int * a, int n )
{
    int i, j, temp;
    i = j = 0;
    if ( !a ) {
        printf("Invalid array parameter\n");
        return 0;
    }
    if ( n < 0 ) {
        printf("Invalid array size\n");
        return 0;
    }
    while ( j < n ) {
        if ( 0 == a[j] ) {
            temp = a[j];
            a[j] = a[i];
            a[i] = temp;
            ++i;
        }
        ++j;
    }
    return 1;
}

1. Time complexity O(N)
2. Space complexity O(1)
3. Tested code till n = 50 million, results correctly in less than 1 second
4. Single pass algorithm
5. Clean and solid

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

private static void SortZerosAndOnes(int[] array, int size) {
    int low = 0; int high = size-1;
    while(low < high) {
        if (array[low] == 0) {
            low++;
            continue;
        }
        if (array[high] == 1) {
            high--;
            continue;
        }
        // now array[low] == 1 and array[high] == 0, change them and move both pointers
        array[low] = 0; array[high] = 1; low++; high--;
    }
}

Note: you can eliminate the size parameter and use array.length-1 as the initial value for high

- Selmeczy, Péter February 13, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

you can use BitVector

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

void sort(int arr[], int size)
{
	int begin = 0;
	int end = size;	
	while(begin < end)
	{
		while(arr[begin] == 0)
		{
			begin++;			
		}

		while(arr[end] == 1)
		{
			end--;			
		}
		
		if(begin < end)
		{
			int temp = arr[begin];
			arr[begin] = arr[end];
			arr[end] = temp;	
		}
	}
}

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

void sort(int arr[], int size)
{
	int begin = 0;
	int end = size;	
	while(begin < end)
	{
		while(arr[begin] == 0)
		{
			begin++;			
		}

		while(arr[end] == 1)
		{
			end--;			
		}
		
		if(begin < end)
		{
			int temp = arr[begin];
			arr[begin] = arr[end];
			arr[end] = temp;	
		}
	}
}

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

This does not work if array is only 0s (or 1s), it will crash with index out of range.
Furthermore swaping 0 and 1 could be simplified (just write 0 to "begin" and 1 to "end"

- Selmeczy, Péter February 13, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Fixed:

void sort(int arr[], int size)
{
        int begin = 0;
        int end = size-1;
        while(begin < end)
        {
                while(arr[begin] == 0)
                {
                        begin++;
                }


                while(arr[end] == 1)
                {
                        end--;
                }

                if(begin < end)
                {
                        int temp = arr[begin];
                        arr[begin] = arr[end];
                        arr[end] = temp;
                }

        }
}

- Biswaranjan February 13, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Sorry, but it is not fixed, the while (arr[begin]==0) begin++; loop nicely increments begin over the size...
while (begin < size && array[begin] == 0) should be the condition, and similar should be added to the 2nd loop (end >= 0 && ...)

- Selmeczy, Péter February 14, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

What is the complexity? O(n)

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

Hi all
Can any 1 will tell wt is prblm with this logic as it is simple n clean solution
============================================================
Go Through the array and count number of zeros(say it countZeros). Now go through again and set it to zero till index countZeros-1. remaining array should be 1.
TC O(n)
space complexity : constant

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

Nothing is wrong with it but it goes through the array twice. In other words the constant factor of O(N) is twice as big as the one-pass solutions. And if there are two O(X) algorithms the one that has smaller constant tag is considered a better one.

- Selmeczy, Péter February 14, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int count = 0;
            int[] numbers = { 1, 0, 1, 0, 1, 0, 0, 1,0, 1, 0, 1 };
            foreach (int num in numbers)
            {
                if (num == 0)
                    count++;
            }
            for (int i = 0; i < numbers.Length; i++)
            {
                if (i <= count)
                {
                    numbers[i] = 0;
                }
                else
                    numbers[i] = 1;
            }

            foreach (int num in numbers)
                System.Console.WriteLine(num);

            System.Console.Read();


        }
        
    }
}

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

ha ha ha much simplier. count all the ones and all the zeros and then re-construct with the zeros first and then the ones.

count0=0;
count1=0;
for(i=0; i< tab.length ; i++)
  if(tab[i]=='0') count0++;
  else count1++;
for(i=0; i<count0; i++) tab[i]='0';
for(j=0; j<count1; j++) tab[i+j]='1';

- MiKL~ February 14, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Sort in-place and wo additional storage. For the sake of brevity, some checkings are omitted.

void sort_zeros_n_ones(int dat[], int count)
{
    int s, e;
    
    s = 0;
    e = count - 1;
    
    /* We want values to be sorted in ascending order, i.e. like "000..11..1" */
    while (s < e) {
        while(dat[s] == 0) {
            s++;
        }        
        while(dat[e] == 1) {
            e--;
        }
        
        if (s < e) {
            swap(dat, s, e);
            s++;
            e--;
        }
    }
}

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

i guess this works for above criteria....

for( i = 0; i<n/2; i++){
if( a[i] < a[n-i-1]){
temp = a[i];
a[i] = a [n-i-1];
a[n-i-1] = temp;
}
}

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

The interviewer seems to have given you more than enough hints about what algorithm to use:

The obvious answer is "Counting sort" because...
1) According to the interviewer, he gave you hints saying that the input will be in the range of 0-100 which means that you can just maintain 100 counters for sorting.
2) Time complexity of this algorithm is O(n) and space complexity is O(k) where n is the number of elements in the input array and k is the number of "distinct" elements in the array (which is 100 in this case).

PS: I don't mean to be rude but before rubbishing this comment, plz go through the counting sort algorithm once and then give you're feedback. :) :)

- Rahul Arakeri March 18, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import com.sun.org.apache.bcel.internal.generic.SWAP;


public class question {

	public static void main(String[] args) {

		int [] arr = new int[30];
		for (int i = 0; i < arr.length; i++) {
			arr[i]= (int)(Math.random()*2);
			System.out.print("  "+ arr[i]);
		}
		System.out.println(" ");
		int i=0,j=29;
		while(i<j){

			for(;i < arr.length; i++) {
				if(arr[i]>0){
					System.out.println(" i " + i);
					break;
				}
			}
			for(;j>0; j--) {
				if(arr[j]<1){
					System.out.println(" j " + j);
					break;
				}
			}
			if(i>j){
				break;
			}else{
				swap(arr,i,j);
			}


		}
		System.out.println(" ");
		System.out.println(" output------>");
		for ( i = 0; i < arr.length; i++) {
			System.out.print("  "+ arr[i]);
		}

	}

	private static void swap(int[] arr, int i, int j) {
		int temp=arr[i];
		arr[i]=arr[j];
		arr[j]=temp;

	}
}

I think the time complexity is o(n)
and space complexity is constant

}

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

for(int i= 0;i<n; i++)
    {
        if(array[i] == 0){
             counter0++;           //this variable is used to count no of zeros
          }
        else
             counter1++;           //this variable is used to count no of ones
    }
for(int i=0; i<counter0;i++)
   {
      array[i]=0;
  }
for(int i= counter0 ; i<n; i++)
    {
       array[i]=1;
     }

- Chetan Angadi October 10, 2012 | 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