Amazon Interview Question for Quality Assurance Engineers


Country: India




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

public static int[] sortPositiveNegative(int []arr) {

int pos=0,neg=0;

int sortedArr[] = Arrays.copyOf(arr,arr.length);

for(int i=0;i<arr.length-1;i++) {

while(arr[pos]<0) {
pos++;
}
sortedArr[i]=arr[pos++];
i++;
while(arr[neg]>0 ) {
neg++;
}
if(i<arr.length)
sortedArr[i]=arr[neg++];
}
return sortedArr;
}

- Vinod November 15, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Can be resolved using PriorityQueue

- Amrish October 25, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

Solution with PriorityQueue is not the best solution, since you using additional space. It can be done without PrirorityQueue using two indexes for pos and neg - O(n) time

- Yavorsky.Yuriy October 31, 2019 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

def sort_positive_negative(arr):
    i = 0
    while i<len(arr)-1:
        if arr[i]>0 and arr[i+1]<0:
            i += 2
        elif arr[i]<0 and arr[i+1]>0:
            arr[i], arr[i+1] = arr[i+1], arr[i]
            i += 2
        else:
            postive = 1
            if arr[i]>0:
                postive = -1
            for j in range(i+2, len(arr)):
                if postive*arr[j]>0:
                    arr[j-1], arr[j] = arr[j], arr[j-1]
                    break
    return arr


if __name__=='__main__':
    data = [
         [
          [-1, 6, 9, -4, -10, -9, 8, 8, 4],
          [6, -1, 9, -4, 8, -10, 8, -9, 4]
         ]
        ]

    for d in data:
        print('input', d[0], 'sort postive-negetive', sort_positive_negative(d[0]), 'expected', d[1])

- Loghman Barari October 27, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

def sort_pos_neg(il):
    for i in range(len(il)):
        if i % 2 == 0:
            # @i expects a positive num
            if il[i] < 0:
                for j in range(i+1, len(il)):
                    if il[j] >= 0:
                        # Swap the items @ i and j
                        il[j], il[i] = il[i], il[j]
                        break
        else:
            # @i expects a negative num
            if il[i] >= 0:
                for j in range(i+1, len(il)):
                    if il[j] < 0:
                        # Swap the items @ i and j
                        il[j], il[i] = il[i], il[j]
                        break

    return il

- lvphuc133 November 08, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

It fails to maintain the insertion order.

- Harry November 13, 2019 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static int[] sortPositiveNegative(int []arr) {
		
		int pos=0,neg=0;
				
		int sortedArr[] = Arrays.copyOf(arr,arr.length);
		
		for(int i=0;i<arr.length-1;i++) {
			
			while(arr[pos]<0) {
				pos++;
			}
			sortedArr[i]=arr[pos++];
			i++;
			while(arr[neg]>0 ) {
				neg++;
			}
			if(i<arr.length)
			sortedArr[i]=arr[neg++];		
		}
		return sortedArr;
	}

- Vinod November 15, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

def function1() :
    for i in range(0,len(a)):
        #print("Element ",a[i])
        if (i % 2 == 0):
            #print("Positive index",i)
            if(a[i]<0) :
                #print("Negative element .. Finding next positive...")
                find_next_positive_swap(i)
        elif (i%2 ==1):
            #print("Negative index",i)
            if(a[i]>0):
                #print("Positive element .. Finding next negative...")
                find_next_negative_swap(i)
    return
            

def find_next_negative_swap(i) :
    for j in range(i,len(a)):
        if(a[j]<0):
            ele=a[j]
            del a[j]
            a.insert(i,ele)
            #print(a)
            return
    return

def find_next_positive_swap(i):
    for j in range(i,len(a)):
        if(a[j]>0) :
           ele=a[j]
           del a[j]
           a.insert(i,ele)
           #print(a)
           return
    return
    

a=[-1, 6, 9, -4, -10, -9, 8, 8, 4]

function1()

a
[6, -1, 9, -4, 8, -10, 8, -9, 4]

- Anonymous November 18, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Ruby

def sort_positive_negative(array)
  pos_arr = []
  neg_arr = []

  while array.size > 0
    if array.first >= 0
      pos_arr << array.shift
    else
      neg_arr << array.shift
    end
  end

  until pos_arr.size == 0 && neg_arr.size == 0
    array << pos_arr.shift if pos_arr.size > 0
    array << neg_arr.shift if neg_arr.size > 0
  end
end

arr = Array.new(10000000) { rand(-10...10) }

Benchmark: 1.310000 0.040000 1.350000 ( 1.409556)

- Andrey Grechko November 18, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

def sort_positive_negative(array)
  pos_arr = []
  neg_arr = []

  while array.size > 0
    if array.first >= 0
      pos_arr << array.shift
    else
      neg_arr << array.shift
    end
  end

  until pos_arr.size == 0 && neg_arr.size == 0
    array << pos_arr.shift if pos_arr.size > 0
    array << neg_arr.shift if neg_arr.size > 0
  end
end

- Andrey Grechko November 18, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#Ascending and descending sorting functionality for neg to positive numbers
def myThirdQ():
print('given mixed elements are getting displayed in sortted order')
a=[1,-5,8,-3,0,7,3,8,9,10]
b = [1,1,0,1,0,1,1,1,1,0,0,1,0,1]
a.sort()
print(a)
b.sort()
print(b)
a.sort(reverse=True)
print(a)
b.sort(reverse=True)
print(b)
myThirdQ()

- Anonymous November 21, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#Ascending and descending sorting functionality for neg to positive numbers
def myThirdQ():
print('given mixed elements are getting displayed in sortted order')
a=[1,-5,8,-3,0,7,3,8,9,10]
b = [1,1,0,1,0,1,1,1,1,0,0,1,0,1]
a.sort()
print(a)
b.sort()
print(b)
a.sort(reverse=True)
print(a)
b.sort(reverse=True)
print(b)
myThirdQ()

- Saranya November 21, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

List<Integer> list = new ArrayList<>(  );
        list.add( -1 );list.add( 6 );
        list.add( 9 );list.add( -4 );
        list.add( -10 );list.add( -9 );
        list.add( 8 );list.add( 8 );list.add( 4 );

        List<Integer> tempList = new ArrayList<>( list );
        List<Integer> result = new ArrayList<>( );
        Collections.sort( tempList , (o1, o2) -> (o1>0)? -1:1 );
       
        int firstNegativeElementIndex = 0;

        for(int i=0; i<tempList.size();i++){
            if(tempList.get( i )<0){
                firstNegativeElementIndex = i;
                break;
            }
        }

        for(int i=0;firstNegativeElementIndex -i>0;i++){
                result.add( tempList.get( firstNegativeElementIndex-1 -i ) );
                if(firstNegativeElementIndex +i < tempList.size())
                result.add( tempList.get( firstNegativeElementIndex +i ) );
        }
        System.out.println(result);

- Chaitanya Srikakolapu November 29, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

let array = [-1,6,9,-4,-10,-9,8,8,4];

function positiveNegativeArray(originalArray){
  let positiveArray = [];
  for(let i of originalArray){
    if(i > 0) positiveArray.push(i);
  }
  
  let negativeArray = [];
  for(let i of originalArray){
    if(i < 0) negativeArray.push(i);
  }

  let pos = 0, neg = 0;
  for(let i=0; i < originalArray.length; i++){
    if(positiveArray && pos > positiveArray.length - 1) positiveArray = null;
    if(negativeArray && neg > negativeArray.length - 1) negativeArray = null;
    
    if((i % 2 === 0 || !negativeArray) && positiveArray){
      originalArray[i] = positiveArray[pos];
      ++pos;
    }else{
      originalArray[i] = negativeArray[neg];
      ++neg;
    }
  }
  return originalArray;
}

positiveNegativeArray(array);

- Hardik Chauhan February 22, 2020 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void positiveNegativeArray(std::vector<int>& v)
{
std::vector<int> orderedVec(v.size());

int posi = 0;
int negi = 0;
for (int i = 0; i < v.size(); ++i) {
if (v[i] >= 0) {
orderedVec[posi * 2] = v[i];
++posi;
}
else {
orderedVec[negi * 2 + 1] = v[i];
++negi;
}
}

v = orderedVec;
}

- Nino, C++ - If it is allowed to allocate a tmp array, this solution has complexity O(N) (assumes count of positive elements- count of negative elements is 0 or 1) March 01, 2020 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

def main():
pos_neg_order()

def pos_neg_roder():
l = [-1, 6, 9, -4, -10, -9, 8, 8, 4, -5,-11]
posList =[]
negList =[]
for i in range(len(l)):
if l[i] > 0 : posList.append(l[i])
else: negList.append(l[i])
myL = zip(posList, negList)

print(posList)
print(negList)
myL = list(myL)
if len(posList)>len(negList): [myL.append(posList[x]) for x in range(len(negList), len(posList))]
if len(negList)>len(posList): [myL.append(negList[x]) for x in range(len(posList), len(negList))]
print(list(myL))

main()

- fk March 05, 2020 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

{def main():
pos_neg_order()

def pos_neg_roder():
l = [-1, 6, 9, -4, -10, -9, 8, 8, 4, -5,-11]
posList =[]
negList =[]
for i in range(len(l)):
if l[i] > 0 : posList.append(l[i])
else: negList.append(l[i])
myL = zip(posList, negList)

print(posList)
print(negList)
myL = list(myL)
if len(posList)>len(negList): [myL.append(posList[x]) for x in range(len(negList), len(posList))]
if len(negList)>len(posList): [myL.append(negList[x]) for x in range(len(posList), len(negList))]
print(list(myL))

main() }

- fk March 05, 2020 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

def main():
    pos_neg_order()
    
def pos_neg_roder():
    l = [-1, 6, 9, -4, -10, -9, 8, 8, 4, -5,-11]
    posList =[]
    negList =[]
    for i in range(len(l)):
        if l[i] > 0 : posList.append(l[i])
        else: negList.append(l[i])
    myL = zip(posList, negList)

    print(posList)
    print(negList)
    myL = list(myL)
    if len(posList)>len(negList): [myL.append(posList[x]) for x in range(len(negList), len(posList))]
    if len(negList)>len(posList): [myL.append(negList[x]) for x in range(len(posList), len(negList))]
    print(list(myL))
    
main()

- fk March 05, 2020 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

def main():
    pos_neg_order()
    
def pos_neg_roder():
    l = [-1, 6, 9, -4, -10, -9, 8, 8, 4, -5,-11]
    posList =[]
    negList =[]
    for i in range(len(l)):
        if l[i] > 0 : posList.append(l[i])
        else: negList.append(l[i])
    myL = zip(posList, negList)

    print(posList)
    print(negList)
    myL = list(myL)
    if len(posList)>len(negList): [myL.append(posList[x]) for x in range(len(negList), len(posList))]
    if len(negList)>len(posList): [myL.append(negList[x]) for x in range(len(posList), len(negList))]
    print(list(myL))
    
main()

- fk March 05, 2020 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

def main():

pos_neg_order()

def pos_neg_roder():

l = [-1, 6, 9, -4, -10, -9, 8, 8, 4, -5,-11]

posList =[]

negList =[]

for i in range(len(l)):

if l[i] > 0 : posList.append(l[i])

else: negList.append(l[i])

myL = zip(posList, negList)

print(posList)

print(negList)

myL = list(myL)

if len(posList)>len(negList): [myL.append(posList[x]) for x in range(len(negList), len(posList))]

if len(negList)>len(posList): [myL.append(negList[x]) for x in range(len(posList), len(negList))]

print(list(myL))

main()

- fk March 05, 2020 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

def main():

pos_neg_order()

def pos_neg_roder():

l = [-1, 6, 9, -4, -10, -9, 8, 8, 4, -5,-11]

posList =[]

negList =[]

for i in range(len(l)):

if l[i] > 0 : posList.append(l[i])

else: negList.append(l[i])

myL = zip(posList, negList)

print(posList)

print(negList)

myL = list(myL)

if len(posList)>len(negList): [myL.append(posList[x]) for x in range(len(negList), len(posList))]

if len(negList)>len(posList): [myL.append(negList[x]) for x in range(len(posList), len(negList))]

print(list(myL))

main()

- fk March 05, 2020 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Recursive Approach

def algo(n=0,p=0,flist=[]):
    
    if n==len(neg)-1:
        flist.extend(pos[n:])
        return flist
    
    if p==len(pos)-1:
        flist.extend(neg[p:])
        return flist
    
    if n<len(neg) and p < len(pos):
        flist.append(pos[n])
        flist.append(neg[n])
        
    
        return algo(n+1,p+1,flist)



l = [-1,6,9,-4,-10,-9,8,8,4]
neg = [ _ for _ in l if _ <0 ]
pos = [ _ for _ in l if _ >=0 ]

print(algo())

- Kishlay April 15, 2020 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void main(String[] args) {
		// TODO Auto-generated method stub

		int arr[] = { -1, 6, 9, -4, -10, -9, 8, 8, 4 };
		int len = arr.length;
		int k[] = new int[len];
		int pos = 0;
		int neg = 1;
		for (int i = 0; i < len; i++) {
			if (arr[i] > 0) {
				k[pos] = arr[i];
				pos = pos + 2;
			}
			if (arr[i] < 0) {
				k[neg] = arr[i];
				neg = neg + 2;
			}
		}
		System.out.println(Arrays.toString(k));
	}

- Prabhu April 30, 2020 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

list_positive = [x for x in list_m if x > 0]
list_nagative = [x for x in list_m if x < 0]
print(list_positive)

def list_nag_order(list_po,list_na):
    ln = len(list_po) + len(list_na)
    result_list = [None] * ln

 
    if len(list_na)<len(list_po):
        for i in range(len(list_na)*2):
            if i % 2 == 0:
                result_list[i] = list_po[int(i/2)]  
            else:
                result_list[i] = list_na[int(i/2)]  

        for x in range(len(list_na)*2,len(result_list)):
            result_list[x] = list_po[int(x/2)]

    if len(list_na) == len(list_po):
        for i in range(len(list_na)*2):
            if i % 2 == 0:
                result_list[i] = list_po[int(i/2)]
            else:
                result_list[i] = list_na[int(i/2)]

    if len(list_na) > len(list_po):
        for i in range(len(list_po)*2):
            if i % 2 == 0:
                result_list[i] = list_po[int(i/2)]  
                result_list[i] = list_na[int(i/2)] 

        for x in range(len(list_po)*2,len(result_list)):
            result_list[x] = list_po[int(x/2)]

    return result_list

print(list_nag_order(list_positive,list_nagative))

- haha July 05, 2020 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

public static int[] sortPositiveNegative(int []arr) {
		
		int pos=0,neg=0;
				
		int sortedArr[] = Arrays.copyOf(arr,arr.length);
		
		for(int i=0;i<arr.length-1;i++) {
			
			while(arr[pos]<0) {
				pos++;
			}
			sortedArr[i]=arr[pos++];
			i++;
			while(arr[neg]>0 ) {
				neg++;
			}
			if(i<arr.length)
			sortedArr[i]=arr[neg++];		
		}
		return sortedArr;
	}

- Anonymous November 15, 2019 | 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