Bazaarvoice Interview Question for Software Engineer / Developers






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

Passing the pointer without size function, it seems impossible to figure out the length of the array. The function has to be in one of following signature:
bool pairExist(int * arr, int size, int sum);
or
bool pairExist(int arr[], int sum);

An O(n) solution is to build a hash by sum-<element> then see if any number appears in the hashset.

- hunt September 30, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

the second function signature is equivalent to the first one and you still need a size parameter.

i don't think i understand your solution. first of all, we are only comparing to one sum element (function parameter), therefore hashing is not necessary - it's a straight compare.
second, you need to go through all possible pair combinations, which means you will have two, nested loops - running time O(n^2).

bool pairExist(int * arr, int size, int sum)
{
for(int i = 0; i < size; ++i)
{
for(int j = i + 1; j < size; ++j)
{
if(sum == (arr[i] + arr[j]))
{
return true;
}
}
}
return false;
}

- Anonymous September 30, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

The second one is different. Since we can figure out size by sizeof(arr)/sizeof(int) to figure out the size of array.

Using hash is to sacrifice some space to reduce time complexity. Brute force, it is O(n^2). Using hash, it use O(n) space but O(n) time.

- hunt September 30, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

You were right about the function signature - thanks for the correction.

I see your logic, but what about the following case:

sum = 10
arr = { 1, 2, 4, 5 };
hashset = { 9, 8, 6, 5 };

Your algorithm would return a false positive. It would have to handle a special case of 0.5 * sum differently.

- Anonymous September 30, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Right. So we could also keep a position in the hash. And we only match when the positions are different.

- hunt September 30, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I don't think it would be necessary. All you need is to count 1/2 sums and not put them in hashset. If the count is >= 2, you got a match.
I like your solution.

- Anonymous September 30, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

can't we have some O(n log n) time solution? First sort the array and then for each element x in arr do binary search for (sum-x).

- Anonymous February 10, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Sorting once seems like it is the solution to many of these type of questions. Then, like mentioned, a binary search for the required difference, or, what first came to me, just stopping the list traversal when the sum is greater than 'sum', since all further results will be greater. There is probably some hybrid with a hash that would reduce clock cycles/runtime further.

Of course, I could be wrong since I devoted more time typing this, than I did on the solution.

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

okay hashing should work giving O(n) time.

- Anonymous February 10, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static boolean isPairExited( int[] ia, int sum)
{
Hashtable hashtable = new Hashtable();
int temp = 0;

for (int i : ia)
{
temp = sum - i;
if (temp >= 0 && hashtable.containsKey(temp))
return true;
hashtable.put(i, true);
}

return false;

}

- cdo2 April 03, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

@hunt @cdo2 @bv I am an IT Recruiter looking for some Software Engineers in the Austin area contact me directly--client has asked that I recruit specifically for Bazaarvoice candidates suzie.jimenez@thehtgroup.com 512.345.9300

- suziejimenez3 September 25, 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