## Bloomberg LP Interview Question Financial Software Developers

• 0

find the 2nd largest number in an integer array, what do you return if the array has only 1 element?

Country: United States
Interview Type: In-Person

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

if array has only one element than we can throw a exception or print a message saying that array has only one element

``````public static int FindSecondNdLarge(int[] array)
{
int size = array.Length;
int[] max = new int[] { 0, 0 };
int counter;

if (array[0] > array[1])
{
max[0] = array[0];
max[1] = array[0];
}
else
{
max[0] = array[1];
max[1] = array[0];
}

for (counter = 2; counter < size; counter++)
{
if (array[counter] > max[1])
{
if (array[counter] > max[0])
{
max[1] = max[0];
max[0] = array[counter];
}
else
{
max[1] = array[counter];
}
}
}

return max[1];``````

}

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

``````static int secondLargest(int *arr, int size)
{
int secondL, largest;
largest = arr[0];
secondL = 0;
for(int i=0; i < size; i++)
{
if(arr[i+1] > largest)
{
secondL = largest;
largest = arr[i+1];
}
else if(arr[i+1] > secondL)
{
secondL = arr[i+1];
}
}
return secondL;
}``````

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

your for loop can only run to (size -1) otherwise when you are running the very last iteration your code will try and access a spot in the array that doesn't exist (arr[i+1])

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

what is the action when there are duplicates? for instance what if number of elements in array is greater then 2 but all elements in the array are equal?

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

``````public class secondLargest {

public static void main(String args[])
{
int arr[] = {100,6,10,7,11,12,3,2,100,17,8,9,100,91,981,5};
int second = findSecond(arr);
System.out.println(" The second largest element in the array is - "+second);
}

//function which returns the second largest element..

public static int findSecond(int arr[])
{
int length =arr.length;
int large,slarge, flag=0;
large=arr[0];
slarge=arr[0];
for(int i=0;i<length;i++)
{
if(large<arr[i])
{
large=arr[i];
}
}
for(int i=0;i<length;i++)
{
if(large!=arr[i])
{
slarge=arr[i];
flag=1;
break;
}
}

//to check whether the array contains only one value or not

if(flag!=1)
{
slarge=large;
return slarge;
}
for(int i=0;i<length; i++)
{
if((slarge<arr[i])  && (arr[i]<large))
{
slarge=arr[i];
}
}
return slarge;
}
}``````

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

We can solve this way: First create a Max-Heap with all the values (Build-Heap takes O(n) time). Then We extract max O(logn) time, and then Maximum (O(1)) is the second largest value. For one value, after extract max heap will be empty and next Maximum call will return null.

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

``````int find2ndmax(const vector<int>& ar)
{
int max = numeric_limits<int>::min();
int max2 = numeric_limits<int>::min();
for (vector<int>::const_iterator ii = ar.begin(); ii != ar.end(); ii++) {
if (*ii > max) {
max2 = max;
max = *ii;
}
}
if (max2 == numeric_limits<int>::min()) {
throw 1; // or return max, depends on what this function is used for
}
return max2;
}``````

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

``````public class SecondMax {

/**
* @param args
*/
public static void main(String[] args) {
SecondMax secondMax = new SecondMax();
int [] array = {10,4,6,4,7};
int secondMaxValue = secondMax.getSecondMax(array);
System.out.println("The second max number in the array  " + secondMaxValue);

}
private int getSecondMax(int[] array){
int max = Integer.MIN_VALUE;
for(int i =0; i<array.length;i++){
if(array[i] > max)
max = array[i];
}
int difference = 0;
int secondMax =0;
int value = Integer.MAX_VALUE;
for(int i =0; i<array.length;i++){
difference = max - array[i];
if(difference < value && difference !=0){
value = difference;
secondMax = array[i];
}
}
return secondMax;
}``````

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

- If the length of array is 1 then throw exception
- If the length of array is 2 then return the array itself
- If the length of array is greater than two then sort the array in desending order using some algorithm and then return the 1st two elements

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

The question asked for second largest number .. not first two largest numbers.

Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

### Books

is a comprehensive book walking you through every aspect of getting a job at a top tech company, while focuses on software engineering interviews.

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