Bloomberg LP Interview Question for Senior Software Development Engineers


Country: United States
Interview Type: In-Person




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

a is the array and k is the given value
1- Sort the array
2- Assign two pointer start=0 and end=a.length-1
3- run loop while start < end
a- if(a[start] + a[end] == k), print pair values at start and end index, start++,end--
b- else if(a[start] + a[end] < k) , start++
c- else if(a[start] + a[end] > k) , end--

Time complexity O(nlogn), The above logic will work with all types of data.

- azambhrgn May 09, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

1- In first pass use a hashmap to add all elements to that hashmap.
2- in the second pass. check if the hashmap.Contains(Sum-array[i]), If yes you have the pair.

Time Complexity O(n)

Psuedo code. You can use C# generics or C++ template to make it generic.

var dict = new Dictonary(int,int)
   for(int i = 0; i < array.length; i ++)
{
    if(!dict.Contains(array[i])) 
   {
       dict.Add(array[i],array[i])
   }
}

for(int i = 0 ; i < array.length;  i++)
{
    if(dict.Contains(Sum-arr[i]))
    {
              Console.Writeline( "{" + arr[i] + "," + (Sum-arr[i]).ToString() + "}");
    }
}

~Cheers

- techbits May 14, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

package org.test.combo;

public class TestCombo {
	public static void main(String[] args) {
		int num = 7;

		int[] numArr = { 2, 1, 9, 5, 3, 6 };
		for (int i = 0; i <= numArr.length - 1; i++) {
			for (int j = i + 1; j <= numArr.length - 1; j++) {
				if ((numArr[i] + numArr[j]) == num) {
					System.out.print("[(" + numArr[i] + "," + numArr[j] + ")]");
				}
			}
		}

	}
}

- JamshedK May 18, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

package org.test.combo;

public class TestCombo {
	public static void main(String[] args) {
		int num = 7;

		int[] numArr = { 2, 1, 9, 5, 3, 6 };
		for (int i = 0; i <= numArr.length - 1; i++) {
			for (int j = i + 1; j <= numArr.length - 1; j++) {
				if ((numArr[i] + numArr[j]) == num) {
					System.out.print("[(" + numArr[i] + "," + numArr[j] + ")]");
				}
			}
		}

	}
}

- JamshedK May 18, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

if repetition is allowed in the input data set, bellow

template <typename T>
std::vector<std::pair<T, T>> printEqualPairs2(std::vector<T> vec, int sum)
{
std::vector<std::pair<T, T>> ret;
std::multiset<T> mulSet;
for (auto i : vec)
mulSet.emplace(i);

for (auto i : vec)
{
auto it = mulSet.find(sum - i);
if (it != mulSet.end())
{
std::pair<T, T> pair(i, *it);
ret.push_back(pair);
mulSet.erase(it);
auto it2 = mulSet.find(i);
if (it2 != mulSet.end())
mulSet.erase(it2);
}
}
return ret;
}

- janitha June 11, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

template <typename T>
std::vector<std::pair<T,T>> findPairs(const std::vector<T>& first, T k)
{
    std::unordered_set<T> firstSet(first.begin(), first.end());
    std::vector<std::pair<T,T>> result;
    
    for(const auto& val: first)
    {
        auto found = firstSet.find(k-val);
        if( found != firstSet.end())
        {
            result.emplace_back(std::pair<T,T>(*found, val));
            firstSet.erase(*found);
            firstSet.erase(val);
        }
        
    }
    return result;
    
}

int main()
{
    
    std::vector<int> first={1,2,3,4,5,7};
    std::vector<std::pair<int,int>> result = findPairs( first, 3);
    
    for( const auto& ele: result)
    {
        std::cout << " first: " << ele.first << "second: "<< ele.second << "\n";
    }

}

- Anonymous June 28, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void findSumHashMap(int[] arr, int k){
		
	    Map<Integer, Integer> pairs = new HashMap<Integer, Integer>();

	    for(int i=0;i<arr.length;i++){
	    	
	    	if(pairs.containsKey(arr[i])){
	    		
	    		System.out.println(arr[i]+", "+pairs.get(arr[i]));
	    	}else{
	    		
	    		pairs.put(k-arr[i], arr[i]);
	    	}
	    }
	}

- Anonymous July 29, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void findSumHashMap(int[] arr, int k){
		
	    Map<Integer, Integer> pairs = new HashMap<Integer, Integer>();

	    for(int i=0;i<arr.length;i++){
	    	
	    	if(pairs.containsKey(arr[i])){
	    		
	    		System.out.println(arr[i]+", "+pairs.get(arr[i]));
	    	}else{
	    		
	    		pairs.put(k-arr[i], arr[i]);
	    	}
	    }
	}

- ag91375 July 29, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

In python.

Correct me if am wrong

import json
x1=input("Enter the list:")
x=json.loads(x1)
y=int(input("enter the sum value:"))
setted=set()
#sett=set()
def sum_array(list1,value):
    x=0
    for i in range(0,len(list1)):
        for j in range(0,len(list1)):
            if i!=j:
                x=list1[i]+list1[j]
                if x==value:
                    setted.add((list1[i],list1[j]))
                   
    print("The repeated item is pair is ",setted)
sum_array(x,y)

- jagannathans92 February 20, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

<T> void pairs(final T[] nums, T sum) {
	if (nums == null) return;
	final Set<T> numsLeft = Arrays.stream(nums).boxed().collect(Collectors.toSet());
	
	for (int i = 0; i < nums.length; ++i) {
		final T diff = sum - nums[i];
		if (numsLeft.contains(diff)) {
			System.out.println(String.format("%d, %d", nums[i], numsLeft.get(diff)));
			numsLeft.remove(nums[i]);
			numsLeft.remove(diff);
		}
	}
}

- jtcgen April 05, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

const sumPairs = (arr, sum) => {
    let pairs = [];
    for(let i=0; i<(arr.length - 1); i++) {
        for(let j=i+1; j<arr.length; j++) {
            if(arr[i] + arr[j] === sum) {
                pairs.push([arr[i], arr[j]]);
            }
        }
    }
    
    return pairs;
};

var arr = [ 2, 1, 9, 5, 3, 6 ];
var sp = sumPairs(arr, 9);
console.log(sp)

- TimH August 17, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

const sumPairs = (arr, sum) => {
    let pairs = [];
    for(let i=0; i<(arr.length - 1); i++) {
        for(let j=i+1; j<arr.length; j++) {
            if(arr[i] + arr[j] === sum) {
                pairs.push([arr[i], arr[j]]);
            }
        }
    }
    
    return pairs;
};

var arr = [ 2, 1, 9, 5, 3, 6 ];
var sp = sumPairs(arr, 9);
console.log(sp)

- TimH August 17, 2018 | Flag Reply


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