## Amazon Interview Question for Quality Assurance Engineers

• 0

Country: India
Interview Type: In-Person

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

Can be solved by using the merge sort merging logic.

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

int[] ar1 = { 1,3,5,7,9,11,45,67};
int[] ar2 = { 2,4,6,8,9,11,34,56,789,789,999,1000};
int[] result;
int maxarrlen = (ar1.Length > ar2.Length) ? ar1.Length : ar2.Length ;
int minarrlen = (ar1.Length > ar2.Length) ? ar2.Length : ar1.Length ;

result = new int[maxarrlen+minarrlen];
int counter = 0;
int total=maxarrlen + minarrlen;
for (int i = 0; i < maxarrlen; i++)
{
if (i < minarrlen)
{
if (ar1[i] >= ar2[i])
{
result[counter++] = ar2[i];
result[counter++] = ar1[i];
}
else
{
result[counter++] = ar1[i];
result[counter++] = ar2[i];
}
}
else
{
result[counter++] = ar2[i];
}
}
foreach (var item in result)
{
Console.Write(string.Format("{0},", item));
}

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

int[] A={2,2,4,6,10,14,20};
int[] B={2,3,3,4,8,10};

int lengthC = A.length + B.length;
if(lengthC==0)
{ System.out.println("Length of Both Arrays is 0"); }
else
{
int[] C = new int[lengthC];
for(int i=0,j=0,k=0;k<lengthC;i++,j++,k++)
{
if(j==B.length)
{
C[k]=A[i];
j--;
}
else
if(i==A.length)
{
C[k]=B[j];
i--;
}
else
{
if(A[i]<=B[j])
{
C[k]=A[i];
j--;
}
else
{
C[k]=B[j];
i--;
}
}
System.out.print(C[k]+" ");
}
}

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

func Merge(A, B []int) []int {
C := make([]int, len(A)+len(B))
ia := 0
ib := 0
ic := 0
for ia < len(A) || ib < len(B) {
if ia == len(A) {
C[ic] = B[ib]
ib++
ic++
continue
}
if ib == len(B) {
C[ic] = A[ia]
ia++
ic++
continue
}
if A[ia] < B[ib] {
C[ic] = A[ia]
ia++
} else {
C[ic] = B[ib]
ib++
}
ic++
}
return C
}

func main() {
A := []int{3, 6, 6, 7}
B := []int{3, 3, 8, 10}
fmt.Println(Merge(A, B))
}

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

My implementation in Java

static int[] mergeTwoSortedArrays(int[] a, int[] b) {
// Corner cases
if (a == null && b == null) {
return null;
}
if (a == null) {
return Arrays.copyOf(b, b.length);
}
if (b == null) {
return Arrays.copyOf(a, a.length);
}

// Common cycle
int[] c = new int[a.length + b.length];

int i = 0, j = 0, k = 0;
while (i < a.length && j < b.length) {
if (a[i] < b[j]) {
c[k] = a[i];
i++;
} else {
c[k] = b[j];
j++;
}
k++;
}

// ends
if (i == a.length) {
while (j < b.length) {
c[k++] = b[j++];
}
} else {
while (i < a.length) {
c[k++] = a[i++];
}
}

return c;
}

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

public static int[] sort(int[] arr1, int[] arr2) {

if (arr1 == null || arr1 == null)
return null;

// indexes
int i1 = 0, i2 = 0, idx = 0;

// new arr length = arr1 + arr2
int[] arr = new int[arr1.length + arr2.length];

// loop until reached end of either array
while(i1 < arr1.length && i2 < arr2.length) {
// add smallest element, increment idxs
arr[idx++] = (arr1[i1] < arr2[i2]) ?
arr1[i1++] : arr2[i2++];
}

// One array will still have data. try both
System.arraycopy(arr1, i1, arr, idx, arr1.length-i1);
System.arraycopy(arr2, i2, arr, idx, arr2.length-i2);

return arr;
}

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

array<int, 3> a { { 1, 3, 7} };
array<int, 4> b { { 0, 2, 4, 6} };

array<int, (a.size()+b.size())> c;

auto cIt = c.rbegin();
auto aIt = a.rbegin();
auto bIt = b.rbegin();

while (true) {
if (aIt == a.rend() || bIt == b.rend())
break;

if (*aIt > *bIt) {
*cIt = *aIt;
cIt++; aIt++;
}
else {
*cIt = *bIt;
cIt++; bIt++;
}
}

while (bIt != b.rend()) {
*cIt = *bIt;
cIt++; bIt++;
}

while (aIt != a.rend()) {
*cIt = *aIt;
cIt++; aIt++;
}

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

import java.io.IOException;
public class sort {
public static void main(String[] args) throws IOException {
String [] str1 = br.readLine().split(" ");
String [] str2 = br.readLine().split(" ");
int size1 = str1.length;
int size2 = str2.length;
int []array1= new int[size1];
int []array2= new int[size2];
for(int i=0; i<size1; i++){
array1[i] = Integer.parseInt(str1[i]);
}
for(int j=0; j<size2; j++){
array2[j] =Integer.parseInt(str2[j]);
}
int [] result = mergeArray(array1, array2);
for(int value: result){
System.out.print(value+" ");
}
}
public static int [] mergeArray(int [] array1, int [] array2){
int [] result = null;
if(array1==null && array2==null){
return result;
}
if(array1==null){
return array2;
}
if(array2 ==null){
return array1;
}
int size1= array1.length;
int size2 = array2.length;
int k=0, i=0, j=0;
result = new int[size1+size2];
while (i<size1 && j<size2){
if(array1[i]<array2[j]){
result[k++] = array1[i++];
}else{
result[k++] = array2[j++];
}
}
while (i<size1){
result[k++] = array1[i++];
}
while (j<size2){
result[k++] = array2[i++];
}
return result;
}
}

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

a=[1,2,3,3,5,8]
b=[2,3,6,9,10]
d={}
for i in (a+b):
try:
d[i] += 1
except:
d[i] =1
for key in d.keys():
ii=1
while ii <= d[key]:
c.append(key)
ii += 1

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

public static class Merger
{
public static IEnumerable<int> Merge(IEnumerable<int> a, IEnumerable<int> b)
{
if (a == null && b == null)
yield break;

if (a == null)
{
foreach (var item in b)
yield return item;
yield break;
}

if (b == null)
{
foreach (var item in a)
yield return item;

yield break;
}

using (var leftEnumerator = a.GetEnumerator())
using (var rightEnumerator = b.GetEnumerator())
{
var leftHasValue = leftEnumerator.MoveNext();
var rightHasValue = rightEnumerator.MoveNext();

while (leftHasValue || rightHasValue)
{
if (!leftHasValue)
{
var val = rightEnumerator.Current;
rightHasValue = rightEnumerator.MoveNext();
yield return val;
continue;
}

if (!rightHasValue)
{
var val = leftEnumerator.Current;
leftHasValue = leftEnumerator.MoveNext();
yield return val;
continue;
}

var leftValue = leftEnumerator.Current;
var rightValue = rightEnumerator.Current;

if (leftValue <= rightValue)
{
leftHasValue = leftEnumerator.MoveNext();
yield return leftValue;
}
else
{
rightHasValue = rightEnumerator.MoveNext();
yield return rightValue;
}
}
}
}
}

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

public static class Merger
{
public static IEnumerable<int> Merge(IEnumerable<int> a, IEnumerable<int> b)
{
if (a == null && b == null)
yield break;

if (a == null)
{
foreach (var item in b)
yield return item;
yield break;
}

if (b == null)
{
foreach (var item in a)
yield return item;

yield break;
}

using (var leftEnumerator = a.GetEnumerator())
using (var rightEnumerator = b.GetEnumerator())
{
var leftHasValue = leftEnumerator.MoveNext();
var rightHasValue = rightEnumerator.MoveNext();

while (leftHasValue || rightHasValue)
{
if (!leftHasValue)
{
var val = rightEnumerator.Current;
rightHasValue = rightEnumerator.MoveNext();
yield return val;
continue;
}

if (!rightHasValue)
{
var val = leftEnumerator.Current;
leftHasValue = leftEnumerator.MoveNext();
yield return val;
continue;
}

var leftValue = leftEnumerator.Current;
var rightValue = rightEnumerator.Current;

if (leftValue <= rightValue)
{
leftHasValue = leftEnumerator.MoveNext();
yield return leftValue;
}
else
{
rightHasValue = rightEnumerator.MoveNext();
yield return rightValue;
}
}
}
}
}

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

C++ solution. Pretty simple.

#include <vector>
#include <type_traits>
#include <iostream>

template<typename IteratorType, typename ResultType>
void pushRest(IteratorType && it, IteratorType && end, ResultType && result)
{
while(it != end)
result.push_back(*it++);
}

template<typename IteratorType, typename ValueType, typename ResultType>
IteratorType pushUntilGreaterEqual(IteratorType && it, IteratorType && end, ValueType && comparand, ResultType && result)
{
while(it != end)
{
auto value = *it;

if(value >= comparand)
break;

result.push_back(value);
it++;
}

return it;
}

template<typename IteratorType, typename ValueType>
IteratorType skipDuplicates(IteratorType && it, IteratorType && end, ValueType && value)
{
while(it != end && value == *it)
it++;

return it;
}

template<typename T>
typename std::remove_reference<T>::type merge(T &&left, T &&right)
{
typename std::remove_reference<T>::type result;

auto lit = left.cbegin();
auto rit = right.cbegin();

auto lend = left.cend();
auto rend = right.cend();

for(;;)
{
if(lit == lend)
{
// done with left, push rest of right
pushRest(rit, rend, result);
break;
}
else if(rit == rend)
{
// done with right, push rest of left
pushRest(lit, lend, result);
break;
}
else
{
auto leftValue = *lit;
auto rightValue = *rit;

if(leftValue == rightValue)
{
// equal values, push one and advance until not equal
result.push_back(leftValue);
lit = skipDuplicates(lit, lend, leftValue);
rit = skipDuplicates(rit, rend, rightValue);
}
else if(leftValue < rightValue)
{
// left is lower, push values from left until this isn't so
lit = pushUntilGreaterEqual(lit, lend, rightValue, result);
}
else if(rightValue < leftValue)
{
// right is lower, push values from right until this isn't so
rit = pushUntilGreaterEqual(rit, rend, leftValue, result);
}
}
}

return result;
}

int main(void)
{
std::vector<int> a {0, 0, 1, 5, 7, 19, 19, 19, 19, 21};
std::vector<int> b {-50, -45, -12, 19, 19, 19, 19, 19, 19, 19, 20, 22, 5000};

auto result = merge(a, b);

for(auto value : result)
std::cout << value << " ";

std::cout << std::endl;

return 0;
}

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

//Autor : prizkaboom
static public void GetNewSortArray(int A[],int B[])
{

ArrayList C = new ArrayList();
int i=0,j=0;

while(i<A.length && j<B.length)
{

if(A[i]< B[j])
{
i++;

}

if(A[i]>B[j]){

j++;
}

if(A[i]==B[j])
{

j++;
i++;
}

}

while(j<B.length)
{

j++;

}

while(i<A.length)
{

i++;

}
System.out.println(C);

}

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

d={}
d['a']= 1; d['b'] = 2; d['c'] = 3; d['d'] = 4
n = 2
d.keys()[d.values().index(n)]

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

d={}
d['a']= 1; d['b'] = 2; d['c'] = 3; d['d'] = 4
n = 2
d.keys()[d.values().index(n)]

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

#include <iostream>
#include <vector>

using namespace std;

void fv(vector<int>&v)
{
unsigned int i = 0;
while(i<=v.size()) {
if(v[i]==-1) {
++i;
continue;
}
for(unsigned int j=i+1;j<=v.size();j++ ) {
if(v[i]==v[j])
v[j]=-1;
}
++i;
}
}

void pv(vector<int>&v){
for(vector<int>::iterator it=v.begin();it!=v.end();it++)
cout<<*it<<" ";
}

vector<int> merge(vector<int>&v1, vector<int>&v2) {
for(unsigned int i=0;i<=v1.size();i++)
for(unsigned int j=0;j<=v2.size();j++)
{
if(v1[i]==v2[j])
v2[j]=-1;
}

vector<int>v3;
unsigned int i;
if(v1.size()>v2.size())
i=v1.size();
else
i=v2.size();

for(unsigned int j = 0; j<i; j++){
if(v1.size()>j && v1[j]!=-1)
v3.push_back(v1[j]);
if(v2.size()>j && v2[j]!=-1)
v3.push_back(v2[j]);
}

return v3;

}

int main()
{
vector<int> v1 = {1,1,1,2,5};
vector<int> v2 = {2,2,2,5,9,11,19};

fv(v1);
fv(v2);

vector<int>v3 = merge(v1, v2);
pv(v3);

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

Can construct a binary search tree with one array and insert one element at a time from the other array. I guess the insertion complexity would be nlogm where n is the length of the BST array and m of the other.

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

class FindSortedArray():
array1=list()
array2=list()
sortedarray=list()
def __init__(self,array1,array2):
self.array1 = array1
self.array2 = array2
for i in range(len(self.sortedarray)):
if self.sortedarray[i] > elem:
self.sortedarray.insert(i,elem)
return
self.sortedarray.append(elem)

def SortArrays(self):
for i in range(max(len(self.array1),len(self.array2))):
if i <= len(self.array1)-1 :
arry1_element=self.array1[i]
else:
arry2_element=self.array2[i]
continue

if i <= len(self.array2)-1 :
arry2_element=self.array2[i]
else:
arry1_element=self.array1[i]
continue
if arry1_element < arry2_element :
elif arry1_element >= arry2_element:
return self.sortedarray
if __name__ == "__main__":
s=FindSortedArray([5,10,3,9],[1,40,4,30,9,9,9])
print s.SortArrays()

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

int[] ar1 = { 1,3,5,7,9,11,45,67};
int[] ar2 = { 2,4,6,8,9,11,34,56,789,789,999,1000};
int[] result;
int maxarrlen = (ar1.Length > ar2.Length) ? ar1.Length : ar2.Length ;
int minarrlen = (ar1.Length > ar2.Length) ? ar2.Length : ar1.Length ;

result = new int[maxarrlen+minarrlen];
int counter = 0;
int total=maxarrlen + minarrlen;
for (int i = 0; i < maxarrlen; i++)
{
if (i < minarrlen)
{
if (ar1[i] >= ar2[i])
{
result[counter++] = ar2[i];
result[counter++] = ar1[i];
}
else
{
result[counter++] = ar1[i];
result[counter++] = ar2[i];
}
}
else
{
result[counter++] = ar2[i];
}
}
foreach (var item in result)
{
Console.Write(string.Format("{0},", item));
}

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

int[] A={2,2,4,6,10,14,20};
int[] B={2,3,3,4,8,10};
int lengthC = A.length + B.length;
if(lengthC==0) { System.out.println("Length of Both Arrays is 0"); }
else { int[] C = new int[lengthC];
for(int i=0,j=0,k=0;k<lengthC;i++,j++,k++)
{
if(j==B.length)
{
C[k]=A[i];
j--;
}
else
if(i==A.length)
{
C[k]=B[j];
i--;
}
else
{
if(A[i]<=B[j])
{
C[k]=A[i];
j--;
}
else
{
C[k]=B[j];
i--;
}
}
System.out.print(C[k]+" ");
}

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

int[] A={2,2,4,6,10,14,20};
int[] B={2,3,3,4,8,10};

int lengthC = A.length + B.length;
if(lengthC==0)
{ System.out.println("Length of Both Arrays is 0"); }
else
{
int[] C = new int[lengthC];
for(int i=0,j=0,k=0;k<lengthC;i++,j++,k++)
{
if(j==B.length)
{
C[k]=A[i];
j--;
}
else
if(i==A.length)
{
C[k]=B[j];
i--;
}
else
{
if(A[i]<=B[j])
{
C[k]=A[i];
j--;
}
else
{
C[k]=B[j];
i--;
}
}
System.out.print(C[k]+" ");
}
}

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

a1 = [4,6,7,9,10]
a2 = [3,4,8,10,45,67]
d = a1
maxVal=d[len(d) -1]
minVal=d[0]

for i in range(len(a2)):
if a2[i] < minVal:
d.insert(0, a2[i])
elif(a2[i] > maxVal):
d.append(a2[i])
else:
for j in range(i,len(d)):
if a2[i] > d[j] and a2[i] < d[j + 1]:
d.insert(j,a2[i])
break
elif(a2[i] == d[j]):
d.insert(j,a2[i])
break

#quicksort(nl, 0, len(nl) -1)
print(d)

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

a1 = [4,6,7,9,10]
a2 = [3,4,8,10,45,67]
d = a1
maxVal=d[len(d) -1]
minVal=d[0]

for i in range(len(a2)):
if a2[i] < minVal:
d.insert(0, a2[i])
elif(a2[i] > maxVal):
d.append(a2[i])
else:
for j in range(i,len(d)):
if a2[i] > d[j] and a2[i] < d[j + 1]:
d.insert(j,a2[i])
break
elif(a2[i] == d[j]):
d.insert(j,a2[i])
break

#quicksort(nl, 0, len(nl) -1)
print(d)

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

import java.util.*;
class ArrayMerge2<T extends Comparable<T>>
{

public void buildStack(Stack<T> s1,T [] ar1)
{
for(int i=ar1.length-1;i>=0;i--)
{
if(!s1.empty())
{
if(s1.peek()==ar1[i])
{}
else
s1.push(ar1[i]);
}
else
s1.push(ar1[i]);
}

}//buildStack

public void mergeStack(Stack<T> s1,Stack<T> s2,Vector<T> v1)
{
T x,y;
while(!s1.empty()&&!s2.empty())
{
x= s1.peek();
y= s2.peek();
if(x.compareTo(y)==0)
{
x= s1.pop();
y= s2.pop();
}
else if(x.compareTo(y)<0)
{
x= s1.pop();
}
else
{
y=s2.pop();
}
}

}//mergeStack

public static void main(String args[])
{
Stack<Integer> s1=new Stack<Integer>();
ArrayMerge2<Integer> am1=new ArrayMerge2<Integer>();
Integer[] ar1={1,2,3,4,4,5,6,7,8,9,11,13,13,14,15,26};
am1.buildStack(s1,ar1);
System.out.println("Stack contents"+s1);

Stack<Integer> s2=new Stack<Integer>();
Integer[] ar2={1,5,6,7,8,9,10,11,12,13,13,14,15,22,26};
am1.buildStack(s2,ar2);
System.out.println("Stack contents"+s2);

Vector<Integer> v1=new Vector<Integer>(3,2);
//Vector v1=new Vector(3,2);
am1.mergeStack(s1,s2,v1);
System.out.println("Vector contents"+v1);

}

}//end of class

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

private static void merger2Arrays(int[] a, int[] b) {

int[] c = new int[a.length + b.length];
char value = 0;
int counterC = 0;
int counterA = 0;
int counterB = 0;

// loop for Array C to put the char
for (int loopC = 0; loopC < c.length; loopC++) {

// Check if A array is already moved
if (counterA > a.length - 1) {
// then move all B into C
c[loopC] = b[counterB];
counterB++;
}
// Check if B array is already moved
else if (counterB > b.length - 1) {
// then move all B into C
c[loopC] = a[counterA];
counterA++;
}

// If A is smaller or equal to
else if (a[counterA] <= b[counterB]) {
// then add value of a in c
c[loopC] = a[counterA];
// increment the counter of A and C
counterA++;
}
// If B is smaller
else if (a[counterA] > b[counterB]) {
// then add value of a in c
c[loopC] = b[counterB];
// increment the counter of B
counterB++;
} else {
System.out.println("uncovered path");
}

}
for (int d : c) {
System.out.println(d);
}
// End of method
}

public static void main(String[] args) {
int[] a = { 1, 2};
int[] b = { 0, 3 };

merge(a, b);
}

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

Python::

Descending order::
>>> def man(l1, l2):
c = []
for i in l2:
c.append(i)
for j in l1:
c.append(j)
c.sort(reverse=True)
return c

>>> man([2,3,4,2,3], [4,5,6,5,6])
[6, 6, 5, 5, 4, 4, 3, 3, 2, 2]

Ascending Order::
>>> def man(l1, l2):
c = []
for i in l2:
c.append(i)
for j in l1:
c.append(j)
c.sort()
return c

>>> man([2,3,4,2,3], [4,5,6,5,6])
[2, 2, 3, 3, 4, 4, 5, 5, 6, 6]
>>>

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

public class Learn {
public static void main(String args[]) {
int arr1[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
int arr2[] = { 6, 7, 8, 9 };
int arr3[] = new int[arr1.length + arr2.length];
int i = 0, j = 0, k = 0;
while (i < arr1.length && j < arr2.length) {
if (arr1[i] < arr2[j]) {
arr3[k] = arr1[i];
++i;
++k;
} else if (arr2[j] < arr1[i]) {
arr3[k] = arr2[j];
++j;
++k;
} else {
arr3[k] = arr1[i];
++i;
++j;
++k;

}
}
if (j < arr2.length) {
while (j < arr2.length) {
arr3[k] = arr2[j];
++j;
++k;
}
} else {
while (i < arr1.length) {
arr3[k] = arr1[i];
++i;
++k;
}
}
for (int x : arr3) {
System.out.println(x);
}
}
}

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

public class Learn {
public static void main(String args[]) {
int arr1[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
int arr2[] = { 6, 7, 8, 9 };
int arr3[] = new int[arr1.length + arr2.length];
int i = 0, j = 0, k = 0;
while (i < arr1.length && j < arr2.length) {
if (arr1[i] < arr2[j]) {
arr3[k] = arr1[i];
++i;
++k;
} else if (arr2[j] < arr1[i]) {
arr3[k] = arr2[j];
++j;
++k;
} else {
arr3[k] = arr1[i];
++i;
++j;
++k;

}
}
if (j < arr2.length) {
while (j < arr2.length) {
arr3[k] = arr2[j];
++j;
++k;
}
} else {
while (i < arr1.length) {
arr3[k] = arr1[i];
++i;
++k;
}
}
for (int x : arr3) {
System.out.println(x);
}
}
}

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

public class SortedArrayFromGiven2SortedArrays {

public void getArr(int[] array1, int[] array2){
int arr3[] = new int[array1.length + array2.length];
int i = 0, j = 0, k = 0;
while (i < array1.length && j < array2.length) {
if (array1[i] < array2[j]) {
arr3[k] = array1[i];
++i;
++k;
} else if (array2[j] < array1[i]) {
arr3[k] = array2[j];
++j;
++k;
} else if(array2[j]==array1[i]){
arr3[k] = array1[i];
++k;
arr3[k] = array2[j];
++i;
++j;
++k;

}
}
if (j < array2.length) {
while (j < array2.length) {
arr3[k] = array2[j];
++j;
++k;
}
} else {
while (i < array1.length) {
arr3[k] = array1[i];
++i;
++k;
}
}
for (int x : arr3) {
System.out.println(x);
}
System.out.println("");
}
public static void main(String args[]){
SortedArrayFromGiven2SortedArrays s = new SortedArrayFromGiven2SortedArrays();
int[] ar1 = {1,4,5,7,9,12};
int[]ar2 = {2,3,6,7,8,10,11,13,14,15};
s.getArr(ar1, ar2);

int[] ar3 = {};
s.getArr(ar3, ar2);

int[] ar4 = {0,0,0,0,0,0,0,0,0,0,0,0};
s.getArr(ar1, ar4);

int[] ar5 = {-1,4,5,7,9,12};
int[] ar6 = {-3,-2,2,3,6,7,8,10,11,13,14,15};
s.getArr(ar5, ar6);
}
}

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

O(m+n) solution s.t. m is first array's length and n is the second's array length

public static Integer[] merge(Integer[] arr1, Integer[] arr2) {
int n = arr1.length+arr2.length;
Integer[] arr = new Integer[n];
int arr1Index = 0;
int arr2Index = 0;
for(int i=0;i<n;i++) {
if(arr1Index==arr1.length) {
arr[i] = arr2[arr2Index++];
} else if(arr2Index==arr2.length) {
arr[i] = arr1[arr1Index++];
} else if(arr1[arr1Index]<arr2[arr2Index]) {
arr[i] = arr1[arr1Index++];
} else {
arr[i] = arr2[arr2Index++];
}
}
return arr;
}

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

public void newarray()
{
int []a={1,2,3,4,5,6,7,10,100,1000};
int []b={4,5,6,7,10,45};
int x=0;
if (a.length>b.length)
{
int [] c= new int[a.length];
for(int i=0;i<a.length;i++)
{
for(int j=0;j<b.length;j++)
{
if(a[i]==b[j])
{
c[j]=a[i];
x++;
}
}
}
for(int j=0;j<x;j++)
System.out.print(c[j]+"\t");
}
else
{
int [] c= new int[b.length];
for(int i=0;i<b.length;i++)
{
for(int j=0;j<a.length;j++)
{
if(b[i]==a[j])
{
c[j]=b[i];
x++;
}
}
}
for(int j=0;j<x;j++)
System.out.print(c[j]+"\t");
}
}

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

public void newarray()
{
int []a={1,2,3,4,5,6,7,10,100,1000};
int []b={4,5,6,7,10,45};
int x=0;
if (a.length>b.length)
{
int [] c= new int[a.length];
for(int i=0;i<a.length;i++)
{
for(int j=0;j<b.length;j++)
{
if(a[i]==b[j])
{
c[j]=a[i];
x++;
}
}
}
for(int j=0;j<x;j++)
System.out.print(c[j]+"\t");
}
else
{
int [] c= new int[b.length];
for(int i=0;i<b.length;i++)
{
for(int j=0;j<a.length;j++)
{
if(b[i]==a[j])
{
c[j]=b[i];
x++;
}
}
}
for(int j=0;j<x;j++)
System.out.print(c[j]+"\t");
}

}

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

public void newarray()
{
int []a={1,2,3,4,5,6,7,10,100,1000};
int []b={4,5,6,7,10,45};
int x=0;
if (a.length>b.length)
{
int [] c= new int[a.length];
for(int i=0;i<a.length;i++)
{
for(int j=0;j<b.length;j++)
{
if(a[i]==b[j])
{
c[j]=a[i];
x++;
}
}
}
for(int j=0;j<x;j++)
System.out.print(c[j]+"\t");
}
else
{
int [] c= new int[b.length];
for(int i=0;i<b.length;i++)
{
for(int j=0;j<a.length;j++)
{
if(b[i]==a[j])
{
c[j]=b[i];
x++;
}
}
}
for(int j=0;j<x;j++)
System.out.print(c[j]+"\t");
}
}

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

public void newarray()
{
int []a={1,2,3,4,5,6,7,10,100,1000};
int []b={4,5,6,7,10,45};
int x=0;
if (a.length>b.length)
{
int [] c= new int[a.length];
for(int i=0;i<a.length;i++)
{
for(int j=0;j<b.length;j++)
{
if(a[i]==b[j])
{
c[j]=a[i];
x++;
}
}
}
for(int j=0;j<x;j++)
System.out.print(c[j]+"\t");
}

else
{
int [] c= new int[b.length];
for(int i=0;i<b.length;i++)
{
for(int j=0;j<a.length;j++)
{
if(b[i]==a[j])
{
c[j]=b[i];
x++;
}
}
}
for(int j=0;j<x;j++)
System.out.print(c[j]+"\t");
}
}

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

public class array{

public static void main(String[] args)
{
int []a={1,2,3,4,5,6,7,10,100,1000};
int []b={4,5,6,7,10,45};
int x=0;
if (a.length>b.length)
{
int [] c= new int[a.length];
for(int i=0;i<a.length;i++)
{
for(int j=0;j<b.length;j++)
{
if(a[i]==b[j])
{
c[j]=a[i];
x++;
}
}
}
for(int j=0;j<x;j++)
System.out.print(c[j]+"\t");
}

else
{
int [] c= new int[b.length];
for(int i=0;i<b.length;i++)
{
for(int j=0;j<a.length;j++)
{
if(b[i]==a[j])
{
c[j]=b[i];
x++;
}
}
}
for(int j=0;j<x;j++)
System.out.print(c[j]+"\t");
}
}
}

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

class Array{ public void NewArray()
{
int []a={1,2,3,4,5,6,7,10,100,1000};
int []b={4,5,6,7,10,45};
int x=0;
if (a.length>b.length)
{
int [] c= new int[a.length];
for(int i=0;i<a.length;i++)
{
for(int j=0;j<b.length;j++)
{
if(a[i]==b[j])
{
c[j]=a[i];
x++;
}
}
}
for(int j=0;j<x;j++)
System.out.print(c[j]+"\t");
}

else
{
int [] c= new int[b.length];
for(int i=0;i<b.length;i++)
{
for(int j=0;j<a.length;j++)
{
if(b[i]==a[j])
{
c[j]=b[i];
x++;
}
}
}
for(int j=0;j<x;j++)
System.out.print(c[j]+"\t");
}
}
public static void main(String[] args)
{
Array a = new Array();
l.NewArray();
}
}

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

Class array {
public void newarray()
{
int []a={1,2,3,4,5,6,7,10,100,1000};
int []b={4,5,6,7,10,45};
int x=0;
if (a.length>b.length)
{
int [] c= new int[a.length];
for(int i=0;i<a.length;i++)
{
for(int j=0;j<b.length;j++)
{
if(a[i]==b[j])
{
c[j]=a[i];
x++;
}
}
}
for(int j=0;j<x;j++)
System.out.print(c[j]+"\t");
}

else
{
int [] c= new int[b.length];
for(int i=0;i<b.length;i++)
{
for(int j=0;j<a.length;j++)
{
if(b[i]==a[j])
{
c[j]=b[i];
x++;
}
}
}
for(int j=0;j<x;j++)
System.out.print(c[j]+"\t");
}
}
public static void main(String[] args)
{
array l = new array ();
l.newarray();
}

}

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

Class array {
public void newarray()
{
int []a={1,2,3,4,5,6,7,10,100,1000};
int []b={4,5,6,7,10,45};
int x=0;
if (a.length>b.length)
{
int [] c= new int[a.length];
for(int i=0;i<a.length;i++)
{
for(int j=0;j<b.length;j++)
{
if(a[i]==b[j])
{
c[j]=a[i];
x++;
}
}
}
for(int j=0;j<x;j++)
System.out.print(c[j]+"\t");
}

else
{
int [] c= new int[b.length];
for(int i=0;i<b.length;i++)
{
for(int j=0;j<a.length;j++)
{
if(b[i]==a[j])
{
c[j]=b[i];
x++;
}
}
}
for(int j=0;j<x;j++)
System.out.print(c[j]+"\t");
}
}
public static void main(String[] args)
{
array l = new array ();
l.newarray();
}

}

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

Class array {
public void newarray()
{
int []a={1,2,3,4,5,6,7,10,100,1000};
int []b={4,5,6,7,10,45};
int x=0;
if (a.length>b.length)
{
int [] c= new int[a.length];
for(int i=0;i<a.length;i++)
{
for(int j=0;j<b.length;j++)
{
if(a[i]==b[j])
{
c[j]=a[i];
x++;
}
}
}
for(int j=0;j<x;j++)
System.out.print(c[j]+"\t");
}

else
{
int [] c= new int[b.length];
for(int i=0;i<b.length;i++)
{
for(int j=0;j<a.length;j++)
{
if(b[i]==a[j])
{
c[j]=b[i];
x++;
}
}
}
for(int j=0;j<x;j++)
System.out.print(c[j]+"\t");
}
}
public static void main(String[] args)
{
array l = new array ();
l.newarray();
}

}

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

Class array {
public void newarray()
{
int []a={1,2,3,4,5,6,7,10,100,1000};
int []b={4,5,6,7,10,45};
int x=0;
if (a.length>b.length)
{
int [] c= new int[a.length];
for(int i=0;i<a.length;i++)
{
for(int j=0;j<b.length;j++)
{
if(a[i]==b[j])
{
c[j]=a[i];
x++;
}
}
}
for(int j=0;j<x;j++)
System.out.print(c[j]+"\t");
}

else
{
int [] c= new int[b.length];
for(int i=0;i<b.length;i++)
{
for(int j=0;j<a.length;j++)
{
if(b[i]==a[j])
{
c[j]=b[i];
x++;
}
}
}
for(int j=0;j<x;j++)
System.out.print(c[j]+"\t");
}
}
public static void main(String[] args)
{
array l = new array ();
l.newarray();
}

}

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

w

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

l1=[-8,-6,-5,-5,1,2,3,4,4,5,6,8]
l=[1,2,7,100]
l2=[]
i=0
j=0
while(len(l)>0 and len(l1)>0):
if l[i]>l1[j]:
l2.append(l1[j])
l1.pop(j)
else:
l2.append(l[i])
l.pop(i)
if len(l)>0:
l2.extend(l)
elif len(l1)>0:
l2.extend(l1)
for k in range(0,len(l2)):
print l2[k],

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

l1=[-8,-6,-5,-5,1,2,3,4,4,5,6,8,100]
l=[1,2,7,10]
l2=[]
i=0
j=0
while(len(l)>0 and len(l1)>0):
if l[i]>l1[j]:
l2.append(l1[j])
l1.pop(j)
else:
l2.append(l[i])
l.pop(i)
if len(l)>0:
l2.extend(l)
elif len(l1)>0:
l2.extend(l1)
for k in range(0,len(l2)):
print l2[k],

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

A1=[1,2,3,4]

B1=[1,3,5,8]

C1 = []

for i in A1:
if i in B1:
B1.remove(i)
C1.append(i)
else:
C1.append(i)
C1=C1[:]+B1[:]
print(C1[:])

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

a=[3,2,1,4,5,10,9]
b=[13,4,12]
c=[]
len_of_c=len(a)+len(b)
for i in range(0,len_of_c):
try:
if len(a) == 0:
a_m=0
else:
a_m = max(a)
if len(b) == 0:
b_m=0
else:
b_m = max(b)

if  a_m > b_m:
c.append(a_m)
a.remove(a_m)
else:
c.append(b_m)
b.remove(b_m)
except:
print("List Empty")
c.reverse()
print(c)

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

public static void main(String[] args) {
int[] a1 = {1,1,1,2,2,2,2,3,4,4,4};
int[] a2 = {1,2};
ArrayList<Integer> alSortedMerge = new ArrayList<>();
for(int i=0; i<a1.length; i++) {
}
int ptr = -1;
for(int j=0; j<a2.length; j++) {
for(int k=0; k<a1.length; k++) {
if(alSortedMerge.get(k)==a2[j]) {
ptr = k;
break;
}
}
if(ptr>=0) {
ptr = -1;
} else {
}
}
System.out.println(alSortedMerge);
}

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

package Careercup;

import java.util.Arrays;

public class SortAndMerge {

public static void main(String[] args) {
// TODO Auto-generated method stub
int[] a= {10,12,14,15};
int[] b= {2,3,1,4};
int[] c=new int[a.length+b.length];
Arrays.sort(a);
Arrays.sort(b);
int i=0,j=0,m=0;
while(i<a.length&&j<b.length)
{
if(a[i]>b[j])
{
c[m]=b[j];
j++;m++;

}
else if(a[i]<b[j])
{
c[m]=a[i];
m++;i++;

}
else if(a[i]==b[j])
{
c[m]=a[i];
m++;i++;j++;
}
}
while(i<a.length)
{

c[m]=a[i];
m++;i++;

}
while(j<b.length)
{
c[m]=b[j];
m++;j++;

}
for( int s=0;s<c.length;s++)
System.out.println(c[s]);
}

}

Comment hidden because of low score. Click to expand.
-2
of 2 vote

a = [10,10,5,5,6]
b = [4,5,7,5,4]
c = []
for i in a:
for j in b:
if i>j and j not in c:
c.append(j)
else:
c.append(i)
print c

Comment hidden because of low score. Click to expand.

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.

### 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.