HCL Tech | Backend Engineer

hcl tech logo
hcl tech
Backend Engineer
July 10, 20258 reads

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

CustomerAccount
110
111
213
311
414
310
413

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.new
m.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)

Q1
Group Customers by Shared Accounts
Data Structures & Algorithms

Given a list of customer ids and accounts ids we'd like to know how many groups of customers share all the same accounts

CustomerAccount
110
111
213
311
414
310
413

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.

Discussion (0)

Share your thoughts and ask questions

Join the Discussion

Sign in with Google to share your thoughts and ask questions

No comments yet

Be the first to share your thoughts and start the discussion!