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 10 AnswersThe 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. if I well understood the following code should be ok for second question: QUESTION 1: >>> b = [10, 29, 30, 1, 45, 34, 55, 66, 99, 3, 44, 11, 56, 75, 55, 64, 54] >>> for i in range(len(b)): ... if i != 0: ... if b[i] > max: ... old_max = max ... max = b[i] ... elif b[i] > old_max: ... old_max = b[i] ... else: ... max = b[i] ... old_max = b[i] old_max will be the second max number >>> for i in range(1,100): ... a.append(i) >>> for i in a: ... if a[i] == 2: ... print(a[i]) ... elif a[i]%2 != 0 and a[i] != 0: ... print(a[i]) 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]) Show More Responses 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) 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)); 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 def primenumbers(self, nums): for i in range (1, 101): for j in range(2, i): if (i%j)==0: break else: print (i) 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]); 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. def secondLargest(array): mylist = sorted(set(array)) print(mylist) print(mylist.pop(-2)) array = [55, 51, 53, 57, 52] secondLargest(array) |