Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find the inserted element in the list

Tags:

algorithm

In a recent interview I was asked:

If you have 2 lists

listA listB

Each are the size 1000 and contain the same elements 1 - 1000. If an element, N is added to listB how can you determine the value of that element?

I responded correctly by saying to subtract listB from listA and the remainder would be the value.

Then he said what if we just have listB after N was added. How would you determine the value that was added?

I failed to answer this!!!! I should know it but I just cant think of it. The hint he gave was to do something similar as I did in the first problem.

Any suggestions?

like image 709
segFault Avatar asked Oct 21 '11 02:10

segFault


1 Answers

You can exclusive XOR both lists and whatever is new (N) you will have it. This is the answer to the first question.

like image 167
DarthVader Avatar answered Oct 13 '22 13:10

DarthVader