Bloomberg LP Interview Question


Country: United States




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

static int Secondlargest(int[] arr)
        {
            int smax = 0;
            int max = 0;
            int val;

            for (int i = 0; i < arr.Length; i++)
            {
                val = arr[i];
                if (val > max)
                {
                    smax = max;
                    max = val;
                }
                else
                {
                    if (val > smax)
                    {
                        smax = val;
                    }
                } smax; 
        }

- A March 11, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

static int Secondlargest(int[] arr)
        {
            int smax = 0;
            int max = 0;
            int val;

            for (int i = 0; i < arr.Length; i++)
            {
                val = arr[i];
                if (val > max)
                {
                    smax = max;
                    max = val;
                }
                else
                {
                    if (val > smax)
                    {
                        smax = val;
                    }
                }
            }
            return smax; 
        }

- A March 11, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include "stdio.h"
#include<conio.h>
using namespace std;
int main()
{
    int arr[6] = {4,15,6,0,2,3}; 
    int max=0,smax=0;
    for(int i= 0;i<6;i++)
    {
      if(arr[i]>max)
      {
        smax=max;
        max=arr[i];       
       }
       else if(arr[i]>smax)
       {
         smax=arr[i];
       }
    }
    printf("\n%d\n",smax);    
 getch();
 return 0;     
}

- nihaldps March 11, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <iostream>
#include <string>
using namespace std;


int retSecondMax(int a[]){

int sectmp = 0, tmp = a[0];

for(int i =1; i<10; i++){
if(tmp<a[i])
{ sectmp = tmp;
tmp = a[i];
}
}

return sectmp;
}

int main(int argc, char *argv[]) {
int a[10] = {3,4,2,1,8,6,7,9,10};
cout<<retSecondMax(a);
}

- moonlight007 March 13, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

private int FindSecondLargest(int[] array)
        {

            int largest = 0;
            int secondLargest = 0;

            for (int i = 0; i < array.Length; i++)
            {
                if (array[i] > largest)
                {
                    secondLargest = largest;
                    largest = array[i];
                }
                else if (array[i] < largest && array[i] > secondLargest)
                    secondLargest = array[i]; 
            }

            return secondLargest;

}

- Nitin April 13, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

private int FindSecondLargest(int[] array)
        {

            int largest = 0;
            int secondLargest = 0;

            for (int i = 0; i < array.Length; i++)
            {
                if (array[i] > largest)
                {
                    secondLargest = largest;
                    largest = array[i];
                }
                else if (array[i] < largest && array[i] > secondLargest)
                    secondLargest = array[i]; 
            }

            return secondLargest;

}

- Nitin April 13, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int find(int *A, int n) {
int f = A[0], s = A[0];
for (int i = 0; i < n; i++) {
if (A[i] > f) {
s = f;
f = A[i];
} else if ( A[i] > s )
{
s = A[i];
}


}
return s;

}

int main() {
int Ar[] = { 5, 7, 9, 8 };
cout << find(Ar, sizeof(Ar)/sizeof(int));
return 0;
}

- Navi April 22, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int find(int *A, int n) {
	int f = A[0], s = A[0];
	for (int i = 0; i < n; i++) {
		if (A[i] > f) {
			s = f;
			f = A[i];
		} else if ( A[i] > s )
		{
			s = A[i];
		}


	}
	return s;

}

int main() {
	int Ar[] = { 5, 7, 9, 8 };
	cout << find(Ar, sizeof(Ar)/sizeof(int));
	return 0;
}

- Navi April 22, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

typedef pair<int,int>   pair_t;
    typedef vector<int>     vint_t;

    int max2(const vint_t& a)
    {   
        pair_t tmp(a.front(), a.front());
        for_each(++a.begin(), a.end(), [&](vint_t::value_type v) {
            if (v > tmp.first)
            {   
                tmp.second = tmp.first;
                tmp.first = v;
            }   
        }); 
        return tmp.second;
    }

- Vick May 04, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Based upon quick sort algorithm:

Randomized-Select(A[p..r],i) looking for ith o.s.
if p = r return A[p]
q <- Randomized-Partition(A,p,r)
k <- q-p+1 the size of the left partition
if i=k then the pivot value is the answer
return A[q]
else if i < k then the answer is in the front
return Randomized-Select(A,p,q-1,i)
else then the answer is in the back half
return Randomized-Select(A,q+1,r,i-k)


T[n] = O(n)

- Joanna8848 January 16, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int find_second_largest_element(std::vector<int> &vec) {
int max = 0;
int second_largest = 0;

for(int i = 0; i < vec.size(); i++) {
if(vec[i] > max) {
second_largest = max;
max = vec[i];
}
}

return second_largest;
}

- Gerald February 15, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int find_second_largest_element(std::vector<int> &vec) {
	int max = 0;
	int second_largest = 0;

	for(int i = 0; i < vec.size(); i++) {
		if(vec[i] > max) {
			second_largest = max;
			max = vec[i];
		}
	}
	
	return second_largest;
}

- Gerald February 15, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void main(String[] args) {
		SecondMax secondMax = new SecondMax();
		int [] array = {7,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;
		int smax = Integer.MIN_VALUE;
		for(int i =0;i<array.length;i++){
			if(array[i] >max ){
				max= array[i];
			}
			else if(smax< array[i]&& array[i]<max){
				smax = array[i];
			}else continue;
		}
		return smax;

	}

}

- Nits March 17, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int secondlrg(vector<int> & vec){
    int sec=vec[0];
    auto max=vec[0];
    int c=0;
    for (auto k=vec.begin(); k!=vec.end(); ++k) {
        if(*k>max){
            
            if (c==0) {
                ++c;
                max=*k;
            }else{
                sec=max;
                max=*k;
            }
        }else if (*k>sec&&c!=0){
            sec=*k;
        }
    }
    return sec;
}

- andrey November 24, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int secondlrg1(vector<int> & vec){
    std::nth_element(vec.begin(), vec.begin()+1, vec.end(),std::greater<int>());
    return *(vec.begin()+1);
}

- andrey November 24, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

#include<iostream>
using namespace std;

int find( int *A , int n )
{
        int f = A[0] , s = A[0];
        for (int i= 0 ;i < n ; i++ )
        {
                if( A[i] > f )
                {

                        s = f;
                        f = A[i];
                }

        }
return s;

}


int main()
{
        int Ar[] = {1,2,3,4,6};
        cout << find( Ar , 7 );
return 0;
}

- Sasi March 10, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This is not correct. Suppose the array like this [5, 7, 9 ,8], the second largest number is actually 8, but your program gives 7. Current element is larger than current max doesn't mean it is larger than second largest number. So you need one more check.

- Linghui March 10, 2012 | Flag


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