IBM Interview Question for Software Engineer / Developers






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

what does it mean by "add to a constant C"? What I got is take sum of any two elements of array and that sum will be always constant. If it is so, then all the array elements must be equal. You can easily check the equality of all array elements by taking the 1st element and comparing it with the rest n-1 elements in O(n) time.

- Sanjeev May 20, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Sanjeev,

I think you got the question wrong...what if I have 1,2,3,4,5,6,7 in the array..and my constant is "7", then I have two possibilities 1+6 and 4+3

- prudhvi May 26, 2008 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

studens r unable to understand ur program

- Anonymous February 22, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Sort the array first and then :

int[] a = {2,5,12,15,17};
i=0;j=a.length-1;
while(i<j){
if( a[i] + a[j] > C )
j--;
else if ( a[i] + a[j] < C )
i++;
else print a[i],a[j];
}
}

- Anonymous November 08, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

The sorting would take O(nlog n) time, so this solution is incorrect.

- SiD January 24, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

for every number in the array,Suppose the number is x,put the C-x as the index in the Hash table,so when carray on ,check whether the number you are dealing in the hash table if it is there then the function should return true.

- the man above is a fool January 24, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This solution is incorrect too, the running time is O(n square) which is inefficient as per the problem asked.

- SiD January 24, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

How about first subtracting every elements of the array by C/2. Then insert the new array into a hash table using the abs value of each elements. If two numbers are hashed to the same place and they have opposite signs, we found the result.

- pwang234 March 14, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Why do any subtracting at all?

When you are about to hash a[i], just check if C - a[i] is already there, if not, add a[i] the hash.

Your solution will be go through the whole array no matter what.

This one will immediately return on the first hit, which could be nice thing to have.

- T March 14, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

true

- Anonymous December 02, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Let the given array be "elements". Let the length of the array be 'N'. Create another array "check" having C+1 elements initialized to 0.

// Initialize "check" in O(N).
// Also subtract individual "elements" in O(N).

for(i=0; i<N;, i++)
{
if (elements[i]<=C)
{
check[elements[i]] = 1;
elements[i] = C - elements[i];
}
}

//Check for the presence C-"elements" using "check" and the modified "elements"
// Again being done in O(N)

for(i=0; i<N;, i++)
{
if (elements[i]<=C)
if (check[elements[i]] == 1)
// The pair is (elements[i],C-elements[i])
}

All the numbers in "elements" are assumed to be greater than or equal to 0 and C is assumed to be +ve. Note that the above algorithm can be used only if we want 2 positive numbers adding up to C. Even addition of (C+1, -1) can give a result C. However, detection of such pairs using the above methodology would require an unbounded array "check" and so the above method cannot be used.

- PKS May 20, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void addsToC(int a[], int c) {
    map<int, int> map;
    for (int i = 0; i < N; ++i) {
        if (map[c - a[i]]) return true;
        map[a[i]] = c - a[i];
    }
    return false;
}

- mg August 03, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Edit: bool addsToC(.... not void, obviously!

- mg August 03, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

how can you use Map like Array.

- sw January 29, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

how can you use Map like Array.

- sw January 29, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

how can you use Map like Array.

- sw January 29, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

how can you use Map like Array.

- sw January 29, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

how can you use Map like Array.

- sw January 29, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Can you please repeat the question?

- Anonymous January 29, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

:P

- Anonymous March 02, 2010 | 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