Similar Problems

Similar Problems not available

Customer Who Visited But Did Not Make Any Transactions - Leetcode Solution

Companies:

  • amazon

LeetCode:  Customer Who Visited But Did Not Make Any Transactions Leetcode Solution

Difficulty: Easy

Topics: database  

Problem:

Given a list of transactions between customers and a list of customers who visited the store, return a list of customers who visited but did not make any transactions.

Input:

  • List[List[str]] transactions - A collection of transactions containing customer name, date, and transaction amount [customer_name, date, amount].
  • List[str] visitors - A list of customers who visited the store.

Output:

  • List[str] - A list of customers who visited but did not make any transactions.

Example:

transactions = [["John", "2020-01-01", "$100"], ["Mary", "2020-01-01", "$50"], ["John", "2020-01-02", "$100"]]
visitors = ["John", "Mary", "Jane"]

find_customers_with_no_transactions(transactions, visitors) # Output ["Jane"]

Solution:

To solve the problem, we can use a dictionary to keep track of each customer's transactions. We then loop through the list of visitors checking to see if each visitor has not yet made any transactions. At the end of the loop, we return the list of customers who have visited but made no transactions.

def find_customers_with_no_transactions(transactions, visitors):

    # Create a dictionary to track each customer's transactions
    customer_transactions = {}
    for transaction in transactions:
        customer_name, date, amount = transaction
        if customer_name not in customer_transactions:
            customer_transactions[customer_name] = []
        customer_transactions[customer_name].append((date, amount))

    # Loop through the list of visitors, looking for customers with no transactions
    no_transactions_customers = []
    for visitor in visitors:
        if visitor not in customer_transactions:
            no_transactions_customers.append(visitor)
            continue
        if len(customer_transactions[visitor]) == 0:
            no_transactions_customers.append(visitor)
 
    return no_transactions_customers

In the solution code above, we first create a dictionary customer_transactions that stores each customer's transaction history based on the list of transactions we received as input. We do this by looping over each transaction and extracting the customer name, date, and transaction amount.

If the customer does not exist in the dictionary, we create a new list for that customer's transactions, and then append their current transaction to the list.

Next, we loop over the list of visitors and check if each customer has any transactions. If a customer has not made any transactions, we add them to the no_transactions_customers list.

Finally, we return the no_transactions_customers list containing the names of customers who visited but made no transactions.

Customer Who Visited But Did Not Make Any Transactions Solution Code

1