Linkedin Interview Question for Software Engineer / Developers






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

METHOD 1 (Use Sorting)

Algorithm:

hasArrayTwoCandidates (A[], ar_size, sum)
1) Sort the array in non-decreasing order.
2) Initialize two index variables to find the candidate
elements in the sorted array.
(a) Initialize first to the leftmost index: l = 0
(b) Initialize second the rightmost index: r = ar_size-1
3) Loop while l < r.
(a) If (A[l] + A[r] == sum) then return 1
(b) Else if( A[l] + A[r] < sum ) then l++
(c) Else r--
4) No candidates in whole array - return 0



METHOD 2 (Use Hash Map)

This method works in O(n) time if range of numbers is known.
Let sum be the given sum and A[] be the array in which we need to find pair.

1) Initialize Binary Hash Map M[] = {0, 0, …}
2) Do following for each element A[i] in A[]
(a) If M[x - A[i]] is set then print the pair (A[i], x – A[i])
(b) Set M[A[i]]

- vikram patil August 11, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

public static void findNumber(int [] arr, int sumvalue) {
Map hash = new HashMap<Integer, Integer>();
for(int i=0; i< arr.length; i++) {
if(hash.get(arr[i]) == null) {
int x = sumvalue - arr[i];
hash.put(arr[i], x);
if(hash.get(x) != null) {
System.out.println("The two numbers - " + arr[i] + " and " + x);
}

}
}

}

- Madhu August 24, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This algorithm doesn't handle if the sum is exactly double of an int that you encounter. You can add a simple check in the final IF statement to account for this:
if (hash.get(x) ! = null && hash.get(x) != arr[i])

- tc February 04, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

// Sort the array first ..

int arr[] = {-2,0,1,2,3,6,10,19,21,23,43};

int start=0, end = arr.length-1;
int x= 0;

while (start<end) {

if(arr[start]+arr[end]<x)
start++;
else if(arr[start]+arr[end]>x)
end--;
else if(arr[start]+arr[end]==x){
System.out.println("pair " + arr[start] + " " + arr[end]);
break;
}
}

// nlogn solution

- Anonymous August 11, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

METHOD 1 (Use Sorting)

Algorithm:

hasArrayTwoCandidates (A[], ar_size, sum)
1) Sort the array in non-decreasing order.
2) Initialize two index variables to find the candidate
elements in the sorted array.
(a) Initialize first to the leftmost index: l = 0
(b) Initialize second the rightmost index: r = ar_size-1
3) Loop while l < r.
(a) If (A[l] + A[r] == sum) then return 1
(b) Else if( A[l] + A[r] < sum ) then l++
(c) Else r--
4) No candidates in whole array - return 0



METHOD 2 (Use Hash Map)

This method works in O(n) time if range of numbers is known.
Let sum be the given sum and A[] be the array in which we need to find pair.

1) Initialize Binary Hash Map M[] = {0, 0, …}
2) Do following for each element A[i] in A[]
(a) If M[x - A[i]] is set then print the pair (A[i], x – A[i])
(b) Set M[A[i]]

- vikram August 11, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

You only need a set here and not hashmap.

- Sam November 23, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

what if they want all the pairs ??

- Aravind August 21, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

then give all the pairs

- Anonymous May 28, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

/*
 * find all pairs of numbers that sum up to k
 
 * 2341,5 -> {(2,3), (4,1)}
 * 2222,4 -> {(2, 2), (2, 2), (2, 2), (2, 2), (2, 2), (2, 2)}
 * 2, 4 -> {}
 */
 
 void find_sum_pairs(int* vec, int n, int k)
 {
    qsort(vec, n, sizeof(int)); //O(nlog(n))
    int left, right;
    left = 0;
    right = n - 1;
    while(left < right)
    {
        if (vec[left] + vec[right] == k)
        {
            printf("(%d, %d)\n", left, right);
            for (int r = right - 1; r > left; r--)
            {
                if (vec[r] == vec[right])
                {
                    printf("(%d, %d)\n", left, r);
                }
            }
            left++;        
        }
        else if (vec[left] + vec[right] < k)
        {
            left++;
        }
        else
        {
            right--;
        }
    }
 }

- wjian@yahoo-inc.com September 13, 2014 | 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