Number of Trusted Contacts of a Customer - Problem
๐ช E-commerce Customer Trust Network Analysis
Imagine you're working for an online marketplace that wants to analyze customer relationships and trust networks. You have access to three key databases:
- Customers Table: Contains registered customers with their IDs, names, and email addresses
- Contacts Table: Shows each customer's trusted contacts (people they know and trust)
- Invoices Table: Records all purchase invoices with prices and customer associations
Your mission is to create a comprehensive invoice report that shows:
- ๐ Customer name who made each purchase
- ๐ฐ Invoice price
- ๐ฅ Total contacts the customer has
- ๐ค Trusted contacts who are also registered customers
The key insight here is that trusted contacts are those contacts whose email addresses also exist in the Customers table - meaning they're not just random contacts, but actual verified customers of the platform.
Return results ordered by invoice_id for consistent reporting.
Input & Output
example_1.py โ Basic Case
$
Input:
Customers = [[1,'Alice','alice@leetcode.com'],[2,'Bob','bob@leetcode.com'],[13,'John','john@leetcode.com'],[6,'Alex','alex@leetcode.com']]
Contacts = [[1,'Bob','bob@leetcode.com'],[1,'John','john@leetcode.com'],[1,'Jal','jal@leetcode.com'],[2,'Omar','omar@leetcode.com'],[2,'Meir','meir@leetcode.com'],[6,'Alice','alice@leetcode.com']]
Invoices = [[77,100,1],[88,200,1],[99,300,2],[66,400,2],[55,500,13],[44,60,6]]
โบ
Output:
[[77,'Alice',100,3,2],[88,'Alice',200,3,2],[99,'Bob',300,2,0],[66,'Bob',400,2,0],[55,'John',500,0,0],[44,'Alex',60,1,1]]
๐ก Note:
Alice (customer 1) has 3 contacts: Bob (registered), John (registered), and Jal (not registered), so 2 trusted contacts. Bob has 2 contacts but neither Omar nor Meir are registered customers. John has no contacts. Alex has 1 contact (Alice) who is registered.
example_2.py โ No Contacts Case
$
Input:
Customers = [[1,'Alice','alice@test.com'],[2,'Bob','bob@test.com']]
Contacts = []
Invoices = [[1,100,1],[2,200,2]]
โบ
Output:
[[1,'Alice',100,0,0],[2,'Bob',200,0,0]]
๐ก Note:
Both customers have no contacts, so contacts_cnt and trusted_contacts_cnt are both 0 for all invoices.
example_3.py โ All Trusted Contacts
$
Input:
Customers = [[1,'Alice','alice@test.com'],[2,'Bob','bob@test.com'],[3,'Charlie','charlie@test.com']]
Contacts = [[1,'Bob','bob@test.com'],[1,'Charlie','charlie@test.com'],[2,'Alice','alice@test.com']]
Invoices = [[10,500,1],[20,300,2]]
โบ
Output:
[[10,'Alice',500,2,2],[20,'Bob',300,1,1]]
๐ก Note:
Alice has 2 contacts (Bob and Charlie) and both are registered customers. Bob has 1 contact (Alice) who is also a registered customer.
Constraints
- 1 โค customers.length โค 104
- 1 โค contacts.length โค 104
- 1 โค invoices.length โค 104
- All customer_id values are unique
- All invoice_id values are unique
- Email addresses are valid and case-sensitive
Visualization
Tap to expand
Understanding the Visualization
1
Join Invoice with Customer
Connect each invoice to its customer's basic information
2
Count All Contacts
For each customer, count their total number of contacts
3
Identify Trusted Contacts
Count contacts whose emails match registered customers
4
Combine Results
Present comprehensive invoice analysis with trust metrics
Key Takeaway
๐ฏ Key Insight: Use SQL JOINs with correlated subqueries to efficiently combine invoice data, customer information, and trust relationship counts in a single optimized database query.
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code