Summary
I encountered a problem involving grouping customers who share identical sets of accounts, for which I developed a Ruby solution with O(n^2) time complexity.
Full Experience
Problem: sharing account
Given a list of customer ids and accounts ids we'd like to know how many groups of customers share all the same accounts
| Customer | Account |
|---|---|
| 1 | 10 |
| 1 | 11 |
| 2 | 13 |
| 3 | 11 |
| 4 | 14 |
| 3 | 10 |
| 4 | 13 |
For this example:
Customers 1 and 3 share all of their accounts, 10 and 11, so they are a match.
Customers 2 and 4 share account 13 but customer 4 also owns account 14, which customer 2 doesn't. They are not a match.
My solution
def sharedAccounts (customerAccounts) res = Set.newm.keys.each do |customer1| accounts1 = m[customer1] tmp_res = [] m.keys.each do |customer2| next if customer1 == customer2 accounts2 = m[customer2] if accounts1 == accounts2 tmp_res << customer2 end end if !tmp_res.empty? res.add((tmp_res + [customer1]).sort) end end # Time complexity: O(n^2) res.to_a
end
Interview Questions (1)
Given a list of customer ids and accounts ids we'd like to know how many groups of customers share all the same accounts
| Customer | Account |
|---|---|
| 1 | 10 |
| 1 | 11 |
| 2 | 13 |
| 3 | 11 |
| 4 | 14 |
| 3 | 10 |
| 4 | 13 |
For this example:
Customers 1 and 3 share all of their accounts, 10 and 11, so they are a match.
Customers 2 and 4 share account 13 but customer 4 also owns account 14, which customer 2 doesn't. They are not a match.