Monotype Interview Question for Senior Software Development Engineers


Country: India
Interview Type: In-Person




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

Sort the elements of B present in A by counting sort. Sort the remaining using whatever sorting algorithm you want.

In Python 3:

A = [4,2,7,6,8,9,1,3,2,5,6]
B = [6,3,4,1]

Bdict = dict()

for i in B:
    Bdict[i] = 0

for j in A:
    if j in Bdict:
        Bdict[j] += 1

Output = []

for i in B:
    count = Bdict[i]
    for k in range(count):
        Output.append(i)

A.sort()

for j in A:
    if j not in Bdict:
        Output.append(j)

print(Output)

- Victor December 05, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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));
	}

- naren December 05, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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;
}

- Stuart December 06, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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
      }
    )
  }

- sun December 06, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

def sort(a, b):
    key = {n:ind for ind, n in enumerate(b)}
    return sorted(a, key=lambda n: key.get(n, len(key) + n))

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

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);

}
}

- Shoeb Siddiqui December 09, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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 + ",");
			}

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

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));

- shoeb JD March 19, 2015 | 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