Meta Interview Question

First interview question: dutch flag problem. Given three color objects arranged randomly in an array, arrange them such that each object is together and their collective color groups are in the correct order.

Interview Answer

Anonymous

Dec 10, 2014

void sort012(int a[], int arr_size) { int low = 0; int hi = arr_size - 1; int mid = 0; while(mid <= hi) { switch(a[mid]) { case 0: swap(&a[low++], &a[mid++]); break; case 1: mid++; break; case 2: swap(&a[mid], &a[hi--]); break; } } }

2