Groupon Interview Question for Software Engineer / Developers


Country: United States
Interview Type: Phone Interview




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

using hashmap will give o(n) solution
otherwise you can sort and search to get o(nlogn) time complexity
naive search will give o(n^2) time complexity

- Amit July 18, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

If I understand the problem statement correctly, hashes seem like overkill. The parameters of the problem are merely to give the pairs of numbers which equate to a sum - I see no indication that limits the operands to a given set. In this forum, I will make the assumption that results should not include duplicates (e.g. when sum is 4, only return one of 1,3 and 3,1. If I were in an interview, I would ask for clarification. That said, here's a C++ option:

#include <list>
#include <utility> // pair

std::list< std::pair<int,int> > getNumberPairsForSum(int sum)
{
	std::list< std::pair<int,int> > result;

	int a = 0;
	int b = sum;
	while(a <= b)
	{
		result.push_back(std::make_pair(a++,b--));
	}

	return result;
}

- JB May 06, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I meant to say "I see no indication that limits the operators to a given set." in previous post (i.e. should not have said operands).

- JB May 06, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

The solution is HashMap based, O(n)... Say given no is n. At each step, say current no is a, see if :
a. a is present in hashset. If it is present, then print a, n-a.
b. If it is not present save n-a into the hashset.

If you can use O(nlogn) solution sort the array and traverse from left and right. At each step:
a. If the sum is less than n then increment left index.
b. If the sum is greater increment right index.
c. If the sum is equal to n, print the two nos. And decrement left.
f. Repeat the above process. Stop when left > right.

- srikantaggarwal July 18, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Question was asked earlier also. Nice solutions provided at: question?id=21240663

- Murali Mohan July 18, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void sprintPossiblePairs(int n){
		System.out.println("given number is "+n);
		for (int i=1;i<=n/2;i++){
			System.out.println(i+"+"+(n-i));
		}
	}

- Andrew June 09, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void sprintPossiblePairs(int n){
		System.out.println("given number is "+n);
		for (int i=1;i<=n/2;i++){
			System.out.println(i+"+"+(n-i));
		}
	}

- Andrew June 09, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Here is two solution - iterative and hashtable in javascript

var min = 1,
    max = 100,
    i,
    arr = [];

for (i = min; i < max; i++)
{
    arr.push(i);
}

//Yes I know it is already sorted - it just indicate that fact that sorting is required
arr.sort(function(a, b) {
    return a - b;
});
function isPairSumIterative(arr, k) {
    var start = 0,
        end = arr.length - 1;

    while (start < end) {
        var sum = arr[start] + arr[end];
        if (sum === k) {
            return true;
        } else if (sum > k) {
            end--;
        } else {
            start++;
        }

    }
    return false;
}
function isPairSumHashTable(arr, k) {
    var hash = {}, found = false;
        arr.some(function(item){
        var diff =  k - item;

        if (diff >= 0 && hash[diff]) {
            found = true;
            return true;
        } else {
            hash[item] = 0;
        }
    });
    return found || hash[k] !== undefined;
}
console.log(isPairSumIterative(arr, 92));
console.log(isPairSumHashTable(arr, 92));

- nikita.groshin July 24, 2015 | 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