Codeathon02_Haritha
[Question]
Algorithms/Data Structures — [Problem Solving]
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 = {“Big Corp”, “Big Corp”, ”Acme”, “Big Corp”, “Zork” ,”Zork” ,”Abe”, “Big Corp”, “Acme”, “Big Corp” ,”Big Corp”, “Zork”, “Big Corp”, “Zork”, “Zork”, “Big Corp”,” Acme”, ”Big Corp”, “Acme”, “Big Corp”, “Acme”, “Little Corp”, “Nadir Corp”}
“Big Corp had 10 Trades out of 23, which is 43.48% of the Total Trades.”
“Both Acme and Zork had 5 trades, which is 21.74% of the Total Trades.”
“The Little Corp, Nadir Corp and Abe had 1 Trade Each, which is 4.35%…”
“So the Answer is [“”Acme””, “” Big Corp ,””Zork””] (In Alphabetical Order) Because only These Three Companies Placed at least 5% of the Trades.
Function Description
Complete the function most ACTIVE in the Editor Below.
most ACTIVE has the following parameter:
String customers[n]: An Array Customers Names
(Actual Questions Says String Array, But Signatures is List of Strings)
Returns String[] : An Alphabetically Ascending Array
Constraints
• 1 < n < 10⁵
• 1 < Length of customers[] < 20
• The First Character of customers[i] is a Capital English letter.
• All Characters of customers[i] except for the First One are Lowercase.
• Guaranteed that At least One Customer makes at least 5% of Trades.
[Sample Input]
“The First Line contains an integer, n, The Number of Elements in customers.”
“Each Line of the n Subsequent Lines (where 0 s i< n) contains a string, customers[i].”
Sample Case 0 Input For Custom Testing
20
Omega Alpha Omega Alpha Omega Alpha Omega Alpha Omega Alpha Omega Alpha Omega Alpha Omega Alpha Omega Alpha Omega Beta
Function most ACTIVE
customers[] size n = 20
customers[] = [As Provided Above]
[Sample Output]
Alpha
Beta
Omega
Explanation
“Alpha made 10 Trades out of 20 (50% of the Total), Omega made 9 Trades (45% of the Total). and Beta made 1 Trade (5% of the Total).All of them have met the 5% Threshold, so all the Strings are Returned in an Alphabetically Ordered Array.”
Program:
package src;
import java.io.*;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
class ActiveTradersLogic {
public static List<String> mostActive(List<String> customers) {
Map<String, Integer> customerMap = new TreeMap<>(); // TreeMap to store customer
names and their counts
names and their counts
List<String> solutionStr = new ArrayList<>(); // List to store the most active customers
int customerMapSize = customers.size(); // Total customers
for (int i = 0; i < customerMapSize; i++) {
String customerKey = customers.get(i);
customerMap.put(customerKey, customerMap.getOrDefault(customerKey, 0) + 1);
}
for (Map.Entry<String, Integer> entry : customerMap.entrySet()) {
String customerKey = entry.getKey();
Integer customerCount = entry.getValue();
double currentCustomerPercent = (double) customerCount / (double) customerMapSize * 100;
if (currentCustomerPercent >= 5.0) {
solutionStr.add(customerKey);
}
}
return solutionStr;
}
}
public class Codeathon02_Haritha {
public static void main(String[] args) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(System.out));
int customersCount = Integer.parseInt(bufferedReader.readLine().trim());
// Read the number of customers from input
List<String> customers = IntStream.range(0, customersCount)
.mapToObj(i -> {
try {
return bufferedReader.readLine();
} catch (IOException ex) {
throw new RuntimeException(ex);
}
}).collect(Collectors.toList());
//Call the mostActive method to find the most active customers
//Call the mostActive method to find the most active customers
List<String> result = ActiveTradersLogic.mostActive(customers);
result.forEach(customer -> {
try {
bufferedWriter.write(customer + "\n");
} catch (IOException e) {
bufferedWriter.write(customer + "\n");
} catch (IOException e) {
e.printStackTrace();
}
});
bufferedReader.close(); // close the input and output streams
bufferedWriter.close();
}
}
[Explanation of Solution]
ActiveTradersLogic Class:
This class contains the business logic for finding the most active customers.
It defines a static method named mostActive that takes a list of customer names as input and returns a list of the most active customers.
Inside the mostActive method:
It initializes a TreeMap called customerMap to store each customer's name as the key and the count of their occurrences as the value. The TreeMap ensures that customer names are sorted alphabetically.
An empty ArrayList named solutionStr is created to store the names of the most active customers.
It calculates the total number of customers (customerMapSize) by getting the size of the input customers list.
It iterates through the customers list and updates the count of each customer in the customerMap.
For each customer in customerMap, it calculates the percentage of their occurrence in the total number of customers and adds them to solutionStr if their percentage is greater than or equal to 5.0%.
Finally, it returns the solutionStr containing the names of the most active customers.
Codeathon02_Haritha Class (Main):
This class contains the main method and handles input and output.
It uses BufferedReader to read input efficiently and BufferedWriter to write output efficiently.
It reads the number of customers from the standard input and stores it in the customersCount variable.
Using Java streams and the IntStream.range method, it reads customer names one by one from the input and collects them into a List<String> named customers. The try-catch block inside the mapToObj is used to handle potential IOException.
It calls the mostActive method from the ActiveTradersLogic class, passing the customers list as an argument, to find the most active customers.
It iterates through the resulting list of active customers and writes each customer's name to the standard output using BufferedWriter.
Finally, it closes the input and output streams to ensure proper resource management
Haritha .P(Intern),
Guard Ninjas,
Data Shield Team,
Enterprise Minds.

0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home