## Interview Question

Quality Assurance Engineer Interview

-Seattle, WA

Amazon## From a given list of array (Not sorted) find the second largest value Find the prime numbers from the given list of array (1 -100) Test plan for Amazon login screen

## Interview Answers

10 Answers

The second question is pretty simple, and it does relate back to test cases. Think of every number as a different state of a program, and proceed as follows: Create a new list to house all your prime numbers. Add 2, 3, 5, and 7 to it. Then, iterate through your list. If the number you're looking at can be taken to 0 with a modulus of 2, 3, 5, or 7, ignore it. If passes all those tests, add it to your list of primes. This will give you a list of all prime numbers between 0 and 100. The trick is that if you know the largest number in the list, you can find all prime numbers in the list just by knowing all prime numbers less than the square root of your largest number (in this case, sqrt(100) = 10 and we look at 2, 3, 5, and 7. If the list was up to 121, we'd look at 2, 3, 5, 7, and 11.) This both gets you your answer AND minimizes the runtime of your program. I don't have any fancy tricks for the first question, but given that sorting is often time-consuming I would go with something like this: 1: Iterate through the list. Use two variables, a "largest" and "second largest." These two should start at 1 and 0 respectively. 2: With every number N, compare the value of N to "largest" and "second largest." 3: If N is larger than "second largest" and smaller than "largest," replace "second largest" with N. 4: If N is bigger than both "largest" and "second largest," then make "second largest" the value of "largest" and make the value of "largest" equal to N. This is very much a "brute force" solution. It may not be the best, but like I said - I don't know any special tricks for this problem.

J_C_Sanders on

x = [10,21,2,3,4,5,6,7,8,9,12,45,67,88,888,4,4,5,3,3,45,37,34,6,777] y = [0,0] for(i= 0 ;i y[0] ){ y[0]= x[i] } } for(i= 0 ;i y[1] && x[i] !== y[0]){ y[1]= x[i] } } console.log(y[1])

Find the second largest number. without sorting on

var foo = []; var bar = [] for (var i = 1; i <= 100; i++) { foo.push(i); } for (var i = 0; i < foo.length ; i++) { if(isPrime(foo[i])){ bar.push(foo[i]) } } function isPrime(n){ if(n<=1){ return false} if(n<=3){ return true} if(n === 5 || n === 7) { return true} if(n % 2 === 0 || n % 3 === 0 || n % 5 === 0 || n % 7 === 0) {return false} return true; } console.log(bar)

Prime numbers between 1 and 100 on

From a given list of array (Not sorted) find the second largest value: Integer[] i = {20,40,60,30}; List list = new ArrayList(Arrays.asList(i)); System.out.println(list); Collections.sort(list, Collections.reverseOrder()); System.out.println(list); System.out.println(list.get(1));

Usha on

def secondLargest(self, nums): first, second = None, None for i in range(0, len(nums)): if i > first: first, second = i, first elif first>i>second: second = i return i

Anonymous on

very simple to find second largest number in unsorted array. 1. first use sort method to sort given array in ascending order 2. then print length-2 (as last element is at length-1) int[] arr = { 1, 45, 45, 765, 22, 456, 12, 7, 000, 9213, 234551, 65454331, 1213, 56534, 12321}; Arrays.sort(arr); System.out.println("Secound largest number int the array is = "+arr[arr.length-2]);

Anonymous on

import java.util.*; public class SecondLargest { // From a given list of array (Not sorted) find the second largest value public static void main(String[] args) { int arr[] = { 7, 4, 6, 3, 7, 5, 2 }; Set unique = new HashSet(); for (int i = 0; i sortedList = new ArrayList(unique); Collections.sort(sortedList); System.out.println("Secord largest number : " + sortedList.get(sortedList.size() - 2)); } }

Please find the proper answer, it will eliminate all the duplicate 1st then do sorting. on

from random import randint a=[] for i in range (1,10): a.append(randint(0,100)) a.sort() print(a[-2]) for i in range(len(a)): if a[i]%2!=0 and a[i]%3!=0 and a[i]%5!=0: print(a[i]," is a prime number") else: print(a[i]," is not a prime number")

Aravind Satyanarayanan on

what if you have 2 same highest number for example {1,2,3,4,5,8,8} arr[arr.length-2] this will still print the highest number and not the second highest number.

Anonymous on

def primenumbers(self, nums): for i in range (1, 101): for j in range(2, i): if (i%j)==0: break else: print (i)

Anonymous on