Home
Refer
Jobs
Alumni
Resume
Notifications

Implement a function in Java that reads a CSV file with the following columns: transactionId (string), userId (string), amount (decimal), and date (date), and returns the total amount aggregated by user for a given date range. The function should take three parameters: the path to the CSV file, the start date, and the end date. Assume that the CSV file is very large (millions of rows) and needs to be processed efficiently.

🚀 Best Answers Get Featured in our LinkedIn Community based on Your Consent, To Increase Your Chances of Getting Interviewed. 🚀

Implementing a Function in Java to Read and Aggregate a Large CSV File

During the interview for the Software Engineer position at PayPal, you may be asked to implement a function in Java that reads a large CSV file and returns the total amount aggregated by user for a given date range. This task requires efficient processing of the file as it contains millions of rows. Here's a step-by-step guide on how to approach this question:

  1. Start by importing the necessary packages:

  2. import java.io.BufferedReader;
    import java.io.FileReader;
    import java.io.IOException;
    import java.math.BigDecimal;
    import java.time.LocalDate;
    import java.time.format.DateTimeFormatter;
    import java.util.HashMap;
    import java.util.Map;
  3. Create a function that takes three parameters:
    the path to the CSV file, the start date, and the end date:

  4. public Map aggregateTransactions(String filePath, LocalDate startDate, LocalDate endDate) throws IOException {
    Map userAmountMap = new HashMap<>();
    // code for reading and processing the CSV file goes here return userAmountMap;
    }
  5. Next, read the CSV file using BufferedReader:

  6. try (BufferedReader br = new BufferedReader(new FileReader(filePath))) {
    String line;
    while ((line = br.readLine()) != null) {
    // code for processing each row goes here }
    }
  7. For each row, split the line by comma and extract the necessary values:

  8. String[] transactionValues = line.split(",");
    String transactionId = transactionValues[0];
    String userId = transactionValues[1];
    BigDecimal amount = new BigDecimal(transactionValues[2]);
    LocalDate transactionDate = LocalDate.parse(transactionValues[3], DateTimeFormatter.ofPattern("yyyy-MM-dd"));
  9. Check if the transaction date is within the given date range:

  10. if (transactionDate.isAfter(startDate) && transactionDate.isBefore(endDate)) {
    // code for aggregating the amount for each user goes here}
  11. Finally, aggregate the transaction amounts for each user:

  12. BigDecimal userAmount = userAmountMap.get(userId);
    if (userAmount == null) {
    userAmountMap.put(userId, amount);
    }
    else {
    userAmountMap.put(userId, userAmount.add(amount));
    }
  13. Return the aggregated amount for each user:

  14. return userAmountMap;

By following these steps, you can efficiently process and aggregate a large CSV file in Java. It's important to optimize your code for large datasets to avoid performance issues. Good luck with your interview!

References:

© 2024 Referral Solutions, Inc. Incorporated. All rights reserved.