Amazon Interview Question for Software Engineer / Developers






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

I think xor method can be applied even for objects provided , we XOR that attributes of object from which equivalence of object is defined. :)

for. e.g

struct point
{
   int x , int y , int z
};

x = all X xored;
y = all Y xored;
z = all z xored;

- anon March 08, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

What do you mean by "even for objects provided" -> that's only true for special cases. What if my object is of the class -

Class Foo{
    private:
         int c;
         string goo;
    public:
         int d;
         string metoo;
}

For the generic term object, I cannot think beyond dict/map/hashtables. Unless the interviewer boils down the definition of his objects to a level that I can play the XOR trickery.

- oldnewcoder March 08, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I am not clear about the question. why can't you just go over the array and use a hashmap to find the count of each element.It's just O(n). And what is this XOR solution?

- coder March 08, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

The hashmap solution is O(n) in time AND space.
The XOR solution is O(n) time and O(1) space.

- S March 08, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Yes the interviewer wanted the most generic answer, when object could be absolutely any kind of object.When I said I would use hashtable he seemed to agree to it.

- troy March 08, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Each Object would have a different hashcode so it would me mapped to different key. This may be a problem...

If the hashcodes are same then we can still go for XOR on hashcodes of the object . In this case we can save space..ie the problem can be still solved in O(n) time and O(1) space.

- Anonymous March 09, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

What is the XOR Solution?

- Anonymous March 09, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Please describe the xor solution.

- SD March 13, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

int findOddTimesItem(int[] arr)
int result = arr[0];
for(int i =1;i<sizeof(arr);i++)
result = result^arr[i];
for(int i =0;i<sizeof(arr);i++)
if(result == arr[i]) break;
return i;
}

- Renji March 14, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

int findOddTimesItem(int[] arr)
int result = arr[0];
for(int i =1;i<sizeof(arr);i++)
result = result^arr[i];
for(int i =0;i<sizeof(arr);i++)
if(result == arr[i]) break;
return i;
}

- Renji March 14, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Sorry. I should use sizeof(arr)/sizeof(int).

- Renji March 14, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.HashMap;
import java.util.Iterator;


public class odd_count_of_a_number_in_ll {

/**
* @param args
*/

static HashMap<Integer,Integer> hm = new HashMap<Integer,Integer>();

static class node
{
int info;
node link;
public node(int info)
{
this.info=info;
link=null;
}
}

public static node insert(node root,int val)
{
node n= new node(val);
if(root == null)
return n;

node cur=root;
while(cur.link!=null)
cur=cur.link;
cur.link=n;
return root;

}


public static void inserthm(node root)
{
node ptr=root;
while(ptr!=null)
{
int tmp=ptr.info;
if(hm.get(tmp)== null)
hm.put(tmp, 1);
else
hm.put(tmp, hm.get(tmp)+1);
ptr=ptr.link;
}
}


public static int chk()
{
Iterator<Integer> it = hm.keySet().iterator();
int tmp;
while(it.hasNext())
{
int key = it.next();
int val = hm.get(key);
if(val%2 != 0)
return key;
}
return -1;
}


public static void print()
{
Iterator<Integer> it = hm.keySet().iterator();

while(it.hasNext())
{
int key = it.next();
int val = hm.get(key);
System.out.println("key::"+key+" value::"+val);

}

}



public static void main(String[] args)
{
node root = new node(1);
root=insert(root, 2);
root=insert(root, 1);
root=insert(root, 2);
root=insert(root, 3);
root=insert(root, 4);
root=insert(root, 4);


node troot=root;
while(troot!=null)
{
System.out.println("ll ::"+troot.info);
troot=troot.link;
}

inserthm(root);
int tmp1;


print();

tmp1 = chk();
if(tmp1== -1)
{
System.out.println("All values are of even count");
}
else
{
System.out.println("Value that has odd count ::"+tmp1);
}
root=root.link;





}
}

- Nohsib March 15, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Can we XOR the address of the object as every object has unique address.

- Yes!! June 25, 2011 | 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