Pergunta de entrevista da empresa Trifacta

Find the second biggest element from the array

Respostas da entrevista

Sigiloso

6 de nov. de 2018

A simple solution will be first sort the array in descending order and then return the second element from the sorted array. The time complexity of this solution is O(nlogn). A more efficient solution: 1. Initialise two variable as "first" and "second" and set them to INT_MIN an arbitrary value ~ 10^10 and first = second = INT_MIN 2. Start traversing the array 3. if current element > first then update first, that is first = current and second = first else if current second update second that is second = current 4. when finished traversing return second

Sigiloso

6 de nov. de 2018

EDIT: A simple solution will be first sort the array in descending order and then return the second element from the sorted array. The time complexity of this solution is O(nlogn). A more efficient solution: 1. Initialise two variable as "first" and "second" and set them to INT_MIN an arbitrary value ~ 10^10 and first = second = INT_MIN 2. Start traversing the array 3. if current element is > first then update first, that is first = current and second = first else if current second update second that is second = current 4. when finished traversing return second

1

Sigiloso

6 de nov. de 2018

I don't know why greater than and less than sign are not working Test 3 : greater than (>) and less than (<)