Microsoft interview question

1. Implement stack using queue 2. Merge two arrays and sort them in order as specified at runtime. The two arrays may share common entries between them, but the resultant array must not have duplicates.

Interview Answers

Anonymous

23 Mar 2012

Dim integers = {1, 2, 3, "a"} Dim characters = {"a", "b", "c", 1} Dim newArray As New ArrayList Dim count As Integer = 0 For Each item In characters If Not newArray.Contains(item) Then newArray.Add(item) newArray.Add(integers(count)) count += 1 End If Next For Each item In newArray MsgBox(item) Next

Anonymous

5 Nov 2015

def isPresentN(element): for i in range(len(result1)): if (result1[i] == element): return True return False def sortArray(inputA): for j in range(1,len(inputA)): for i in range(len(inputA)-j): if(inputA[i]>inputA[i+1]): temp = inputA[i] inputA[i] = inputA[i+1] inputA[i+1] = temp return inputA def mergeArraySorted(inputArray1 , inputArray2): if (len(inputArray1)==0 and len(inputArray2)==0): return 'Both arrays are empty' elif (len(inputArray1)==0): return inputArray2 elif (len(inputArray2)==0): return inputArray1 print inputArray1,inputArray2 first = 0 second = 0 while ((first < len(inputArray1)) and (second < len(inputArray2))): if ((isPresentN(inputArray1[first])==False) and (isPresentN(inputArray2[second])==False)): result1.append(inputArray1[first]) result1.append(inputArray2[second]) second = second + 1 first = first + 1 elif (isPresentN(inputArray1[first])): if isPresentN(inputArray2[second])==False: result1.append(inputArray2[second]) second = second + 1 elif (isPresentN(inputArray2[second])): if isPresentN(inputArray1[first])==False: result1.append(inputArray1[first]) first = first + 1 else: second = second + 1 first = first + 1 print result1 return sortArray(result1)