Computer Science Canada Interview question: find common element in 3 arrays |
Author: | cool dude [ Wed Jul 11, 2012 5:17 pm ] |
Post subject: | Interview question: find common element in 3 arrays |
This is an algorithm question asked at an interview. You have 3 arrays and for simplicity we'll say they're equal length and SORTED. Example: a1 = {2, 4, 7, 9} a2 = {4, 5, 6, 11} a3 = {1, 3, 4, 7} The task is to find the number of common elements between all 3 arrays. So in this example the output will be 1 since there is only one common element in all 3 arrays which is 4. Obviously you can solve this using 3 for loops and iterate through each array. You can even solve with 2 for loops while iterating through the first array and the inner for loop iterates through the other 2 arrays... However, this is highly inefficient as this has time complexity of O(n^3) and O(n^2) respectively. Does anyone know of any good algorithms for solving this problem? My only efficient algorithm I came up with as follows: 1. Iterate through each element in the array 2. Create Hashtable 3. check if the element is already in the Hashtable 3.1 If it is then increment the value of that element 3.1.1 If that value is 3 then increment our local count since we found a common element in all 3 arrays 3.2 if the element is not in the Hashtable then add it to the Hashtable 4. print count However, this requires the use of a Hashtable meaning we increase our data space. Is there a better or easier way of doing this? |
Author: | Zren [ Wed Jul 11, 2012 6:04 pm ] |
Post subject: | RE:Interview question: find common element in 3 arrays |
Your current idea is flawed. Eg: a1 = {} a2 = {1} a3 = {1, 1} |
Author: | cool dude [ Wed Jul 11, 2012 6:36 pm ] |
Post subject: | Re: RE:Interview question: find common element in 3 arrays |
Zren @ Wed Jul 11, 2012 6:04 pm wrote: Your current idea is flawed.
Eg: a1 = {} a2 = {1} a3 = {1, 1} Your example is flawed as I said all 3 arrays must be same length. Also the elements in each array are unique so you won't have a3 = {1,1} |
Author: | Dreadnought [ Wed Jul 11, 2012 7:22 pm ] |
Post subject: | Re: Interview question: find common element in 3 arrays |
My first instinct would be to keep track of 3 indices, one for each array and a total of course. Starting with the first value in each array, do the following Check if all 3 values are equal, if so increment the total and all indices by 1. Otherwise, increment the index of lowest value by 1. Repeat until you reach the end of one list (at this point you are done) This would use a single loop and at first glance seems reasonably efficient since each iteration guarantees that you advance in one of the lists. This means you have at most 3*n iterations of your loop. |
Author: | chrisbrown [ Wed Jul 11, 2012 7:25 pm ] | ||
Post subject: | Re: Interview question: find common element in 3 arrays | ||
I think this works, generalized to N arrays:
After reaching the end of the first array, commonCount contains the number of elements common to all arrays. Indexes increase monotonically so the running time is O(L*N) for N arrays of length L. Edit: Ha, three minutes too late. |
Author: | cool dude [ Wed Jul 11, 2012 11:41 pm ] | ||
Post subject: | Re: Interview question: find common element in 3 arrays | ||
Wow way simpler than I thought. I guess thinking under pressure is a bit different. Thanks a lot Dreadnought here is the code I wrote using the algorithm you explained:
|
Author: | wtd [ Thu Jul 12, 2012 12:36 am ] | ||
Post subject: | Re: Interview question: find common element in 3 arrays | ||
|
Author: | Raknarg [ Thu Jul 12, 2012 10:44 am ] |
Post subject: | RE:Interview question: find common element in 3 arrays |
I had a thought. If they're aleady sorted, could you just mergesort them together and then use a single for loop to find three arrays elements that are the same in order? |
Author: | wtd [ Thu Jul 12, 2012 11:14 am ] | ||
Post subject: | Re: Interview question: find common element in 3 arrays | ||
I implemented Tony's algorithm as per our discussion of this, which scales to any size dataset, but requires that all arrays be sorted. Arrays are never mutated, so there's a minimal, linearly scaling memory hit. The program also never scrubs over any array element twice.
|
Author: | Amarylis [ Thu Jul 12, 2012 12:51 pm ] | ||
Post subject: | RE:Interview question: find common element in 3 arrays | ||
Go go python power! |
Author: | Tony [ Thu Jul 12, 2012 3:43 pm ] |
Post subject: | Re: RE:Interview question: find common element in 3 arrays |
Amarylis @ Thu Jul 12, 2012 12:51 pm wrote: Go go python power!
Great. What's the runtime complexity of looking up an element by value in array? |
Author: | wtd [ Thu Jul 12, 2012 7:30 pm ] | ||
Post subject: | RE:Interview question: find common element in 3 arrays | ||
If you're going to use Python, at least make use of sets.
Edit: I'm too tired to do the math, but shouldn't this scale reasonably well? The intersection gets simpler with each iteration of reduce, and we're not looping over each array more than once. |
Author: | Amarylis [ Fri Jul 13, 2012 12:14 am ] |
Post subject: | Re: RE:Interview question: find common element in 3 arrays |
Tony @ Thu Jul 12, 2012 3:43 pm wrote: Amarylis @ Thu Jul 12, 2012 12:51 pm wrote: Go go python power!
Great. What's the runtime complexity of looking up an element by value in array? O(N), which in three arrays would be O(N^3), I know, I was just trying to give the simple answer Sorry. (I might be completely wrong on my Big-O notation) |
Author: | Brightguy [ Fri Jul 13, 2012 10:53 am ] |
Post subject: | Re: RE:Interview question: find common element in 3 arrays |
Raknarg @ Thu Jul 12, 2012 10:44 am wrote: I had a thought. If they're aleady sorted, could you just mergesort them together and then use a single for loop to find three arrays elements that are the same in order?
Yes indeed! Nice thought. Amarylis @ Fri Jul 13, 2012 12:14 am wrote: O(N), which in three arrays would be O(N^3), I know, I was just trying to give the simple answer Sorry.
As mentioned, since the third loop isn't nested it would only be O(n^2). wtd @ Thu Jul 12, 2012 7:30 pm wrote: Edit: I'm too tired to do the math, but shouldn't this scale reasonably well? The intersection gets simpler with each iteration of reduce, and we're not looping over each array more than once.
Python uses hash tables to implement sets, so in the worst case (maximum number of collisions) intersection will be O(n^2). It should perform better in practice, though. |
Author: | Raknarg [ Fri Jul 13, 2012 4:56 pm ] |
Post subject: | RE:Interview question: find common element in 3 arrays |
@Bightguy I really don't know how the math works at all, but I'm assuming it would change the complexity from O(n^3) to O(n)*3. I really should look this stuff up... |
Author: | Brightguy [ Fri Jul 13, 2012 5:49 pm ] |
Post subject: | Re: RE:Interview question: find common element in 3 arrays |
Raknarg @ Fri Jul 13, 2012 4:56 pm wrote: @Bightguy I really don't know how the math works at all, but I'm assuming it would change the complexity from O(n^3) to O(n)*3.
Sure, it would be O(3n). But constant factors are irrelevant to Big O, so it's just O(n). |
Author: | wtd [ Mon Jul 16, 2012 1:24 am ] | ||||||||
Post subject: | RE:Interview question: find common element in 3 arrays | ||||||||
The algorithm as seen in my last submission, but implemented without mutable state.
If we actually look at the required calls to solve.
If we modify the program slightly...
Then it breaks down like so:
Essentially now we check to see if removing everything less than the leading number of the first list makes all first elements equal. But, if it doesn't, we remove everything equal to or less than the first element. This saves us some work the next time around. |