Sponsored
Sponsored
This approach involves using SQL to join the `Users` and `Transactions` tables. A GROUP BY clause will be used to calculate the sum of transactions for each account. We will then filter users whose balances exceed 10000. This is a straightforward SQL-based solution that effectively uses database functionalities.
Time Complexity: O(N), where N is the number of transactions. Space Complexity: O(M), where M is the number of users with a balance greater than 10000.
1SELECT U.name, SUM(T.amount) AS balance FROM Users U JOIN Transactions T
The code joins the `Users` table and `Transactions` table using the common `account` column. It then groups the data by `account` and `name` and calculates the total balance using the SUM function on the `amount` column. A HAVING clause filters results to include only users with a balance greater than 10000.
This approach involves programmatically processing the data from tables using a HashMap or Dictionary. First, we iterate over the transactions table to compute the balances. Then, we filter out users whose balances exceed the required threshold. This uses basic data structures and is implemented in multiple programming languages.
Time Complexity: O(N), where N is the number of transactions. Space Complexity: O(M), where M is the number of unique accounts.
1import java.util.*;
2
3public class BankAccountSummary {
4 public static List<String> highBalanceUsers(List<Map<String, Object>> users, List<Map<String, Object>> transactions) {
5 Map<Integer, Integer> balanceMap = new HashMap<>();
6 for (Map<String, Object> trans : transactions) {
7 Integer account = (Integer) trans.get("account");
8 Integer amount = (Integer) trans.get("amount");
9 balanceMap.put(account, balanceMap.getOrDefault(account, 0) + amount);
10 }
11
12 List<String> result = new ArrayList<>();
13 for (Map<String, Object> user : users) {
14 String name = (String) user.get("name");
15 Integer account = (Integer) user.get("account");
16 Integer balance = balanceMap.getOrDefault(account, 0);
17 if (balance > 10000) {
18 result.add(name);
19 }
20 }
21
22 return result;
23 }
24
25 public static void main(String[] args) {
26 List<Map<String, Object>> users = Arrays.asList(
27 new HashMap<String, Object>() {{ put("account", 900001); put("name", "Alice"); }},
28 new HashMap<String, Object>() {{ put("account", 900002); put("name", "Bob"); }},
29 new HashMap<String, Object>() {{ put("account", 900003); put("name", "Charlie"); }}
30 );
31
32 List<Map<String, Object>> transactions = Arrays.asList(
33 new HashMap<String, Object>() {{ put("trans_id", 1); put("account", 900001); put("amount", 7000); put("transacted_on", "2020-08-01"); }},
34 new HashMap<String, Object>() {{ put("trans_id", 2); put("account", 900001); put("amount", 7000); put("transacted_on", "2020-09-01"); }},
35 new HashMap<String, Object>() {{ put("trans_id", 3); put("account", 900001); put("amount", -3000); put("transacted_on", "2020-09-02"); }},
36 new HashMap<String, Object>() {{ put("trans_id", 4); put("account", 900002); put("amount", 1000); put("transacted_on", "2020-09-12"); }},
37 new HashMap<String, Object>() {{ put("trans_id", 5); put("account", 900003); put("amount", 6000); put("transacted_on", "2020-08-07"); }},
38 new HashMap<String, Object>() {{ put("trans_id", 6); put("account", 900003); put("amount", 6000); put("transacted_on", "2020-09-07"); }},
39 new HashMap<String, Object>() {{ put("trans_id", 7); put("account", 900003); put("amount", -4000); put("transacted_on", "2020-09-11"); }}
40 );
41
42 // Result should be ["Alice"]
43 System.out.println(highBalanceUsers(users, transactions));
44 }
45}
This Java program uses a HashMap to track the balance of each account based on transactions. It iterates over the transaction list to populate the balance Map. Later, it checks each user against the balance Map and adds users with a balance over 10000 to the results list. The main function demonstrates usage of this method.