Directi Interview Question


Country: India
Interview Type: Phone Interview




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

Use Dutch Flag Algo:

1. left = 0, mid = 0, right = n
2. while mid < right
3. if arr[mid] == 0 swap arr[left] and arr[mid], then mid++;
4. if arr[i] == 1 mid++;
5. if arr[i] == 2 swap arr[mid] and arr[right], then right--;
geeksforgeeks will have more details.

- artemis November 08, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Good job, but you'll need 'left++' in 3rd statement as well

- Rail.Suleymanov November 08, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Oh yes..thanks for pointing that out...
left should mark the end of sorted 0s

- artemis November 08, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I tried like as you mentioned, its failing for the input {1,1,2,2,2,0,1,2,0}, if I use while(mid<right), but working fine if I use while(mid<=right), can you please check once?

- Andi November 09, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

yes it should be equal to..Thanks for pointing that out.
I hope this works fine for all.

1. left = 0, mid = 0, right = n
2. while mid <= right
3. if arr[mid] == 0 swap arr[left] and arr[mid], then mid++; left++;
4. if arr[i] == 1 mid++;
5. if arr[i] == 2 swap arr[mid] and arr[right], then right--;
geeksforgeeks will have more details.

- artemis November 09, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Hmmm, now it seems to be correct, but my implemented code still displays wrong result:

def DNFAlgo(a) :
	right = len(a) - 1
	if right < 1 : return
	left = 0
	mid = 0
	while mid <= right :
		if a[mid] == 0 :
			a[left], a[mid] = a[mid], a[left]
			mid = mid + 1
			left = left + 1
		if a[mid] == 1 :
			mid = mid + 1
		if a[mid] == 2 :
			a[mid], a[right] = a[right], a[mid]
			right = right - 1

for your array {1,1,2,2,2,0,1,2,0} it displays result:
{0,0,1,1,2,1,2,2,2}

- Rail.Suleymanov November 09, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think I've discovered my problem: need to use 'elif' in 2 last conditions...

- Rail.Suleymanov November 09, 2012 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

You could try this:

arr = [0,2,2,0,2,1]

zeroFreq, oneFreq, twoFreq = 0, 0, 0
newList = []

for i in xrange(len(arr)):
  if arr[i] == 0: 
    zeroFreq += 1
  elif arr[i] == 1: 
    oneFreq += 1
  else: 
    twoFreq += 1

for i in xrange(zeroFreq):
  newList.append(0)
for i in xrange(oneFreq):
  newList.append(1)
for i in xrange(twoFreq):
  newList.append(2)

print newList

- Ali November 08, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

Dutch Flag Algorithm

- A2 November 08, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

Following-up question: Assume counting is not allowed, how to do it in O(n)?

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

1. Find number of '0', '1' and '2'
2. Create new array (or replace old) knowing how many '0's, '1's and '2's should be there.

- Rail.Suleymanov November 08, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

scan full array and count 0 and 1 then print 0 and 1 counted number of times then print rest as 2.

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

crackprogramming.blogspot.com/2012/11/three-color-sort-sort-array-containing.html

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

Solution loops through the array once keeping track of the minimum index for 0,1,2. If the number at the previous index if greater than the current index number, then we swap the current index number with the minimum index of the number at previous index.

static void sort(int[] array) {
  if (array == null || array.length < 1)
   return;
  int[] lastIndex = new int[3];
  lastIndex[0] = -1;
  lastIndex[1] = -1;
  lastIndex[2] = -1;
  System.out.println("lastIndex(" + Arrays.toString(lastIndex) + ") array("+ Arrays.toString(array) + ")");
 for (int i = 0; i < array.length; i++) {
  if (lastIndex[array[i]] == -1)
   lastIndex[array[i]] = i;
  if (i != 0) {
   int current = array[i];
   int prev = array[i - 1];
   if ((prev - current) > 0) {
    if ((prev - current) == 1 || lastIndex[1] == -1) {
     swap(i, lastIndex[prev], array);
     updateLastIndex(current, prev, i, lastIndex, array);
     System.out.println("lastIndex(" + Arrays.toString(lastIndex) + ") array("+ Arrays.toString(array) + ")");
    } else {
     // swap 1 and 0
     swap(i, lastIndex[1], array);
     updateLastIndex(0, 1, i, lastIndex, array);
     System.out.println("lastIndex(" + Arrays.toString(lastIndex) + ") array("+ Arrays.toString(array) + ")");
     // swap 2 and 1
     swap(i, lastIndex[prev], array);
     updateLastIndex(current, prev, i, lastIndex, array);
     System.out.println("lastIndex(" + Arrays.toString(lastIndex) + ") array("+ Arrays.toString(array) + ")");
     }
    }
   }
  }
 }
static void updateLastIndex(int current, int previous, int index, int[] lastIndex, int[] array) {
 if (lastIndex[current] > lastIndex[previous])
  lastIndex[current] = lastIndex[previous];
 if (array[lastIndex[previous] + 1] == previous)
  lastIndex[previous]++;
 else
  lastIndex[previous] = index;
}
static void swap(int i, int j, int[] array) {
 array[j] ^= array[i];
 array[i] ^= array[j];
 array[j] ^= array[i];
}

- CodeSpace November 08, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Just use binary sorting.

- Coders November 09, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

that is of O(nlogn) complexity whereas Dutch flag is O(n).

- artemis November 09, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Just use buble sorting...

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

May be you can use counting sort.

public static int[] getSortedArray ( int[] inputArr ) {
		
		/*
		 * We already know that the unsorted Array has just 
		 * 0s, 1s, 2s. We can leverage this information. 
		 */
		
		if ( inputArr == null ) {
			return null;
		}
		int[] intermediateArr = new int[3];
		 
		int i;
		for ( i = 0 ; i < inputArr.length ; i++ ) {
			intermediateArr[inputArr[i]]++; 
		}
		
		int count = 0;
		for ( i = 0 ; i < intermediateArr.length; i++ ) {
			while ( intermediateArr[i] > 0 ) {
				inputArr[count] = i;
				count++;
				intermediateArr[i]--;
			}
		}
		
		return inputArr;
	}

- belligerentCoder November 09, 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