hcl tech logo
HCLTech· Backend Engineer

HCL Tech | Backend Engineer

Posted
10 Jul 2025
271 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.

How it went

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

Questions asked (1)

  1. 01

    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.

Know someone preparing? Send it to them:WhatsAppLinkedInX

Interviewed at HCLTech recently?

Write up your rounds while you still remember the questions. It helps the next person more than you would think.

Share your experience
Discussion 0

Questions for the author, or how your own interview compared.

Join the discussion

Sign in with Google to comment or ask the author a question.

No comments yet

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