Amazon Interview Question for Software Engineer Interns


Country: United States
Interview Type: Phone Interview




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

As mentioned by Ayush, if there are no space constraints, a "map/hashtable" can do the job in O(n) where "n" is the length of the list (in fact the number of distinct numbers on the list). I added a simple java function for that.

public static void Solve(Iterator<Integer> numbers, int K) {
        HashMap<Integer, Integer> table = new HashMap<>();
        while (numbers.hasNext()) {
            Integer new_number = numbers.next();            
            table.put(new_number, K - new_number);
            int target = K - new_number;
            if (table.containsKey(target)) {
                System.out.print("Found the pair (" + new_number.toString() + 
                        ", " + Integer.toString(target) + ")\n");
            }            
                
        }
    }

Example:

public static void main(String[] args) {
        // TODO code application logic here
        List<Integer> l = new LinkedList<>();
        for (int i = 1; i < 21; i++)
            l.add(i);
        SumKonLL.Solve(l.iterator(), 13);
                
    }

Output:

Found the pair (7, 6)
Found the pair (8, 5)
Found the pair (9, 4)
Found the pair (10, 3)
Found the pair (11, 2)
Found the pair (12, 1)

- Ehsan January 14, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Won't this fail for duplicate key ?
e.g. 1 2 4 5 ...

- Rucha January 20, 2014 | Flag
Comment hidden because of low score. Click to expand.
3
of 3 vote

public static void findPair(int [] array , int k){
		HashMap<Integer,Integer> map = new HashMap<Integer,Integer>();
		
		for (int i =0 ; i<array.length;++i){
			if (map.containsKey(array[i])){				
				System.out.println(map.get(array[i])+ " : " + array[i]);
			}else{
				map.put(k-array[i], array[i]);
			}
		}

}

- Scott January 14, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 3 vote

use a hashtable

time complexity O(n)
space complexity O(MAX(list)-MIN(list)+1)

- gdg January 14, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Interesting. I would ask the interviewer on space constraints. Based on the requirements, I'd go for your suggestion or insertion sort with O(n^2):O(1) time:space complexities.

- Nishant Kelkar January 16, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 2 vote

//suppose that integer is 5
		LinkedList<Integer>test=new LinkedList<Integer>();
		//adding some elements
		test.add(1);
		test.add(2);
		test.add(4);
		test.add(3);
		test.add(6);
		test.add(5);
		for (Integer no1 : test) {
			if(test.contains(5-no1))
				System.out.println("OUTPUT:"+no1+" "+(5-no1));
		}

- KP January 14, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Previous answer is O(n^2). This one is O(n*logn), assuming that find a number in a set takes logN.

void findPair(list<int> aList, int sum)
{
	set<int> aSet;

	for each (auto item in aList)
	{
		if (aSet.find(sum - item) != aSet.end())
		{
			cout << item << " " << (sum - item) << endl;
		}
		aSet.insert(item);
	}

}

- Grr1967 January 14, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

How about converting the array to a BST and then for a node(say root node) in the bst search for some other node(say target node) whose value when added with the reference node will give the desired sum. Now delete the reference node and target node . Continue this process untill last 2 nodes are present. Every search will happen in O(log m) where m is number of elements in BST. With each iteration value of m will decrease by a factor of 2 as we will delete the reference node. If space is not a constraint then we can improve the results further by using DP.

- Sandeep Reddy January 15, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

With each iteration number of nodes will decrease by 2 as we will delete reference and target nodes.

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

In Python:

def findSumToK(L, k):

    map = {}

    node = L.head
    while node:
        map[node.data] = node
        node = node.next

    node = L.head
    while node:
        i = k - node.data
        if map.get(i):
            return map.get(i), node
        node = node.next

- thugli February 11, 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