Amazon Interview Question for Software Engineer / Developers


Country: United States
Interview Type: Phone Interview




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

Your solution is good however you can mention some other solutions like:

1- If equal arrays mean they are identical (i.e they have the same numbers at the same positions) then it can be done in O(n) and O(1) space just by looping in one of the arrays and compare each element to its corresponding element in the other array.

2- If you can know the range of the numbers in the array (like numbers are from 0 to N-1) then you will need an additional array of the same size instead of a hash table which will give you a faster access. By the additional array you can loop on one array, count the occurrences of each element then loop on the second array and subtract the occurrences. If the additional array has zero at each position then they are equal otherwise they are not. This requires four passes (initialize the temp array, count the occurrences of the first array, subtract the occurrences of the second array and finally check the temp array).

3- You can sort both arrays and apply approach #1 O(N log N)

4- You can use a hash table just like you did.

Ofcourse a comparison of the lengths of the both arrays can save much effort.

Hope that helps you .

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

Modification for case 2
If range is from 0 to (n-1) without extra array,you can find whether two arrays are equal or not

- Amit June 21, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 2 vote

AMZ 10
------

Input:  two arrays
Output: boolean stating their equality

Assumptions:
- Equal in length
- Equal in values
  - What if values are custom classes?
    - Trust that their equality function works?
    - Are the objects comparable?
    
Braintorm:
1.
- If length is equal
  - Sort both arrays, then compare each element

O(nlogn) time, O(1) space

2.
- Add first array to hashMap
- Check if each element from 2nd array exists in hashMap, if it does, remove from hashMap
- If hashMap is empty at the end, arrays are equal

O(n) time, O(n) space

public boolean areEqual(T[] a, T[] b) {
    if(a.length != b.length)
        return false;
    
    if(!a.getClass().equals(b.getClass())
        return false;
    
    Arrays.sort(a /* May need to pass comparator */);
    Arrays.sort(b /* May need to pass comparator */);
         
    for(int i = 0; i < a.length; i++) {
        if(!a[i].equals(b[i]))
            return false;
        //Can use a[i].compareTo(b[i]) != 0 if that's implemented
        //Or whichever equality/comparison function is appropriate
    }
    return true;
}

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

class Test
{
	public static void main(String[] args)
	{
		int[] arr1 = {1,2,3,4};
		int[] arr2 = {1,2,3,4};
		
		boolean flag = true;
		
		for(int i=0; i < arr1.length; i++)
		{
		   	if(arr1[i] != arr2[i])
		   	{
		   		flag = false;
		   	}
		}
		
		if(flag)
		  System.out.println("Arrays are equal");
		else
		  System.out.println("Arrays are not equal");  
	}
	
}

- Anonymous June 11, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

did not get the logic how hashtable helps ... ??

- Atul Kumar June 11, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

hashTable works fine .... but is there any assumptions that in both array values may be in different sequence ???

- Atul Kumar June 12, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I guess we can use XOR operation here...
--First perform XOR inside each of the arrays independently---o(n)
--Then perform a XOR of the result of the two.

If the final result is zero--this means the arrays are equal.

int xor1=0,xor2=0;
 for(int i=0;i<arr1.length;i++){
 	xor1^=arr1[i];
 }
 for(int j=0;j<arr2.length;j++){
 	xor2^=arr2[j];
}
int xor = xor1^xor2;

if(xor>0) printf("Arrays are not equal");

- peechus June 12, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

A={4.2}
B={0.6}
Your code will make them equal
Xor won't work

- Amit June 20, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Thank you Amit.

- peechus June 20, 2013 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

how about use stack and push the items of first array and pop the items of second array.
If the stack is empty then arrays are equal

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

That would only work when both the arrays are sorted.

- Epic_coder June 07, 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