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.
1def high_balance_users(user_data, transaction_data):
2 balance_map = {}
3 for trans in transaction_data:
4 account = trans['account']
5 amount = trans['amount']
6 if account not in balance_map:
7 balance_map[account] = 0
8 balance_map[account] += amount
9
10 result = []
11 for user in user_data:
12 name = user['name']
13 account = user['account']
14 balance = balance_map.get(account, 0)
15 if balance > 10000:
16 result.append((name, balance))
17
18 return result
19# Example usage
20users = [{'account': 900001, 'name': 'Alice'}, {'account': 900002, 'name': 'Bob'}, {'account': 900003, 'name': 'Charlie'}]
21transactions = [
22 {'trans_id': 1, 'account': 900001, 'amount': 7000, 'transacted_on': '2020-08-01'},
23 {'trans_id': 2, 'account': 900001, 'amount': 7000, 'transacted_on': '2020-09-01'},
24 {'trans_id': 3, 'account': 900001, 'amount': -3000, 'transacted_on': '2020-09-02'},
25 {'trans_id': 4, 'account': 900002, 'amount': 1000, 'transacted_on': '2020-09-12'},
26 {'trans_id': 5, 'account': 900003, 'amount': 6000, 'transacted_on': '2020-08-07'},
27 {'trans_id': 6, 'account': 900003, 'amount': 6000, 'transacted_on': '2020-09-07'},
28 {'trans_id': 7, 'account': 900003, 'amount': -4000, 'transacted_on': '2020-09-11'},
29]
30# Result: [('Alice', 11000)]
31print(high_balance_users(users, transactions))
This Python function expects a list of users and a list of transactions. It creates a dictionary mapping accounts to their balances by iterating through the transactions. It then checks each user, computes their balance, and adds those users who have a balance greater than 10000 to the result list. This is returned and can be printed or processed further.