Barclays Interview Question

Write code to return a sorted array of int, when passed in 2 sorted array of int to the method.

Interview Answers

Anonymous

Jan 15, 2010

consider using only 1 loop

1

Anonymous

Jun 8, 2011

1 loop only if 2 it becomes n2

1

Anonymous

May 24, 2010

use loop meaning 1 loop? or 1 inside of another?

Anonymous

Feb 24, 2010

the efficient way would be: using a loop, pick item from array1 and array2. compare them, and add smallest into a new arrayList., followed by the higher. eventually, convert this arrayList into an int array using arrayList.toArray)int[arraylist.size()]). why adding into third array? if we pick one from first array and insert into the second array at appropriate place, it will push all subsequent elements and try to increase arrayList2's capacity. these pushing back and increasing are expensive operations. hence creating a new arrayList and adding guarantees a one time iteration.