Header Ads Widget

Active Traders certification test problem | Hackerrank Solution

 Problem:-

  An institutional broker wants to review their book of customers to see which are most active. Given a list of trades by customer name, determine which customers account for at least 5% of the total number of trades. Order the list alphabetically ascending by name.

Example
n = 23
customers = ["Bigcorp", "Bigcorp", "Acme", "Bigcorp", "Zork", "Zork", "Abc", "Bigcorp", "Acme", "Bigcorp", "Bigcorp", "Zork", "Bigcorp", "Zork", "Zork", "Bigcorp", "Acme", "Bigcorp", "Acme", "Bigcorp", "Acme", "Littlecorp", "Nadircorp"].

Bigcorp had 10 trades out of 23, which is 43.48% of the total trades.

Both Acme and Zork had 5 trades, which is21.74% of the total trades. 

The Littlecorp, Nadir, and Abc had 1 trade each, which is 4.35% of the total trades.

So the answer is ["Acme", "Bigcorp", "Zork"] (in alphabetical order) because only these three companies placed at least 5% of the trades.

Function Description

Complete the function mostActive in the editor below.
mostActive has the following parameter: string customers[n]: an array customer
names
Returns
string]: an alphabetically ascending array of customer names
Constraints
• 1≤n≤ 105
• 1 slength of customers[1] < 20
• The first character of customers[i] English letter is a capital
• All characters of customers[i] except for the first one are lowercase English letters.
• It is guaranteed that at least one customer makes at least 5% of trades.

Code:-

def mostActive(customers):
    dic={}
    n=len(customers)
    for i in customers:
        if i not in dic:
            dic[i]=1
        else:
            dic[i]+=1
    ans=[]
    for i in dic:
        a=(dic[i]*100)/n
        if(a>=5):
            ans.append(i)
    ans.sort()
    return ans
    

if __name__ == '__main__':
    fptr = open(os.environ['OUTPUT_PATH'], 'w')

    customers_count = int(input().strip())

    customers = []

    for _ in range(customers_count):
        customers_item = input()
        customers.append(customers_item)

    result = mostActive(customers)

    fptr.write('\n'.join(result))
    fptr.write('\n')

    fptr.close()



Recommended Post:

Key points:-

Cracking the coding interview:-

 Array and string:-

Tree and graph:-

Hackerearth Problems:-

Hackerrank Problems:-

Data structure:-

 MCQs:-