Amazon Interview Question for SDE1s


Country: United States
Interview Type: In-Person




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

print(int a[], int n)
{
 std::set<int> arr;

 std::set::iterator<int> it;

 for(int i = 0 ; i <n ; i++)
 {
  it = arr.find(sum - a[i]);
  if(it != arr.end())
	   cout<<a[i]<<" "<<*it;
  else
	  arr.insert(a[i]);
 }

}

- Putta June 03, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 4 vote

bool hasArrayTwoCandidates(int A[], int arr_size, int sum)
{
    int l, r;
 
    /* Sort the elements */
    quickSort(A, 0, arr_size-1);
 
    /* Now look for the two candidates in the sorted 
       array*/
    l = 0;
    r = arr_size-1; 
    while(l < r)
    {
         if(A[l] + A[r] == sum)
              return 1; 
         else if(A[l] + A[r] < sum)
              l++;
         else // A[i] + A[j] > sum
              r--;
    }    
    return 0;
}

- Putta June 03, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This code only works when there is no duplicate

- Jackie June 07, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

The following code returns the indexes of the pairs which their sum is K.

public static int [] findParis(int [] array, int k){
		int [] indexes = new int [2];
		indexes[0]= indexes[1] =-1;
		Hashtable<Integer, Integer> hash = new Hashtable<Integer, Integer>();
		for (int i = 0; i < array.length; i++) {
			if(hash.containsKey(array[i])){
				indexes[0] = hash.get(array[i]);
				indexes[1] = i;
				break;
			}
			hash.put(k-array[i], i);
		}
		return indexes;
	}

- ahmad.m.bakr June 03, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Solution using HashSet.

Input:  Array of ints, int k
Output: Print all pairs from array, that will sum to k

Brainstorm:
- Brute force solution can check every possible pair to see if sum is 1, and print it out; HashSet may be necessary if dupication is necessary ... O(n^2)
- Another approach is:
  - insert all number to hashmap
  - then iterate through number array linearly
    - for each number, check if sum - number exists in hashset
      - if exists, print (number, hashsetKey) and remove the key from hashset
- This appoach O(n) with O(n) space

public void findPairs(int [] nums, int k) {
    HashSet<Integer, Boolean> hs = new HashSet<>();
    for(int i = 0; i < nums.length; i++) {
        hs.put(nums[i], true);
    }
    
    for(int i = 0; i < nums.length; i++) {
        if(hs.containsKey(k-nums[i])) {
            System.out.println("(" + nums[i] + "," + k - nums[i] + ")");
            hs.remove(k-nums[i]);
        }
    }
}

- Rooney June 06, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
Comment hidden because of low score. Click to expand.
0
of 0 votes

Huh?

- Anonymous June 03, 2013 | Flag


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