Conversation
eric-andeen
left a comment
There was a problem hiding this comment.
Good code.
What other solutions did you consider? This is the brute-force way of solving the problem, and it works, but time complexity is less than ideal. Can you do better without sacrificing space complexity? You should at least discuss in your comments, even if you don't code another solution.
| # Space complexity: O(n), where n is less than m | ||
| def intersection(array1, array2) | ||
| raise NotImplementedError | ||
| return [] if array1 == nil || array2 == nil |
There was a problem hiding this comment.
nil might be more appropriate if one of the input arrays is nil. The problem description doesn't specify, so your choice is still valid.
| return [] if array1 == nil || array2 == nil | ||
|
|
||
| i = 0 | ||
| inter_array = [] |
There was a problem hiding this comment.
Not a great variable name. Choose a name that tells the reader what it's for.
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| # Time complexity: O(n*m), where n is the number of elements in array1 and m is the number of elements in array2 | ||
| # Space complexity: O(n), where n is less than m |
There was a problem hiding this comment.
Except n is not necessarily less than m. A more accurate statement would be: O(min(n,m)). Also, not that that's the worst case; when one array is a subset of the other. The typical case will be smaller.
No description provided.