Monotype Interview Question
Senior Software Development EngineersCountry: India
Interview Type: In-Person
Approach: Create the sort order using the second array using the map which stores map->index map. Then look up this order during the sorting. Entry in the map is given higher order. If the entries are not there in the order use the natural order.Code here in Java
private static void sortArrayBasedOnAnother(Integer [] array,Integer [] sortOrderArray ) {
//create a hashmap which stores the index of element as the value : value->index map
//from the sortOrderArray
final Map<Integer,Integer> sortOrderMap = new HashMap<Integer, Integer>();
for (int i = 0; i < sortOrderArray.length; i++) {
sortOrderMap.put(sortOrderArray[i],i);
}
Arrays.sort(array, new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
// TODO Auto-generated method stub
Integer order1 = sortOrderMap.get(o1);
Integer order2 = sortOrderMap.get(o2);
if(order1!=null && order2!=null) return order1-order2;
if(order1!=null && order2==null) return -1;
if(order1 ==null && order2!=null) return 1;
return o1-o2;
}
});
}
public static void main(String[] args) {
//A = {4,2,7,6,8,9,1,3,2,5,6}
//B = {6,3,4,1}
Integer [] A = new Integer [] {4,2,7,6,8,9,1,3,2,5,6};
Integer [] B = new Integer [] {6,3,4,1};
sortArrayBasedOnAnother(A, B);
System.out.println(Arrays.toString(A));
}
public static int[] sort(int[] a, int[] b)
{
int index = 0;
for(int i = 0; i< b.length;i++)
{
for(int j = i; j< a.length;j++)
{
//swap
if(a[j] == b[i])
{
int temp = a[j];
a[j] = a[index];
a[index] = temp;
index++;
}
}
}
//sort remaining elements
Arrays.sort(a, index, a.length);
return a;
}
def sort(a: List[Int], b: List[Int]) : List[Int] = {
val order = b.zipWithIndex.toMap
a.sortWith((e1, e2) =>
if(order.contains(e1) && order.contains(e2)) {
order(e1) < order(e2)
} else if(order.contains(e1) && !order.contains(e2)) {
true
} else if(!order.contains(e1) && order.contains(e2)) {
false
} else {
e1 < e2
}
)
}
List<Integer> list1=new ArrayList<Integer>();
list1.add(3);
list1.add(2);
list1.add(5);
list1.add(4);
int count=0;
List<Integer> list2=new ArrayList<Integer>();
list2.add(3);
list2.add(2);
list2.add(5);
list2.add(4);
list2.add(3);
list2.add(5);
list2.add(2);
list2.add(4);
Iterator<Integer> it=list1.iterator();
while (it.hasNext()) {
int n=(Integer)it.next();
System.out.print(n+" ");
for (int i = 0; i < list2.size(); i++) {
if(n==list2.get(i)){
Collections.swap(list2, count, i);
count++;
}
}
}
System.out.println(list2);
}
}
int[] A = { 4, 2, 7, 6, 8, 9, 1, 3, 2, 5, 6 };
Array.Sort(A);
ArrayList A1 = new ArrayList(A);
int[] B = { 6, 3, 4, 1 };
ArrayList O = new ArrayList();
for (int i = B.Length - 1; i >= 0; i--)
{
while (A1.Contains(B[i]))
{
A1.Remove(B[i]);
O.Insert(0, B[i]);
}
}
O.AddRange(A1);
foreach (int x in O)
{
Console.Write(x + ",");
}
int A1[]={4,2,7,6,8,9,1,3,2,5,6};
int A2[]={6,3,4,1,9,7};
Arrays.sort(A1);
int temp=0;int index=0;
for (int i = 0; i < A2.length; i++) {
for (int j = 0; j < A1.length; j++) {
if (A2[i]==A1[j]) {
A1[index]=A1[j]+A1[index];
A1[j]=A1[index]-A1[j];
A1[index]=A1[index]-A1[j];
index++;
}
}
Arrays.sort(A1, index+1, A1.length);
}
System.out.println(Arrays.toString(A1));
Sort the elements of B present in A by counting sort. Sort the remaining using whatever sorting algorithm you want.
In Python 3:
- Victor December 05, 2014