Similar Problems

Similar Problems not available

Confirmation Rate - Leetcode Solution


  • amazon

LeetCode:  Confirmation Rate Leetcode Solution

Difficulty: Medium

Topics: database  

The Confirmation Rate problem on LeetCode requires finding the percentage of confirmation for each user, given their order data.

The problem statement can be summarized as follows: given a list of order data in the format (userid, orderid, action, timestamp), where action is either "confirmed" or "cancelled", find the confirmation rate for each user. The confirmation rate is defined as the percentage of orders that a user has confirmed out of all of the orders they have attempted (i.e. the sum of confirmed and cancelled orders).

A possible solution to this problem involves using a dictionary to store the user data and calculating the confirmation rate for each user. Here's a step-by-step solution:

  1. Create a dictionary to store the user data. The keys of the dictionary will be the user IDs, and the values will be a list of tuples representing each order attempt. Each tuple will contain the order ID, action, and timestamp.

  2. Iterate through the order data and update the dictionary accordingly. For each order attempt, check if the user ID already exists in the dictionary. If it does, append the order data to the corresponding list. If it doesn't, create a new list for the user and add the order data to it.

  3. Once the dictionary has been populated, iterate through it and calculate the confirmation rate for each user. For each user, count the number of confirmed orders and the total number of order attempts (confirmed + cancelled). Calculate the confirmation rate as the percentage of confirmed orders out of the total order attempts.

  4. Store the confirmation rate for each user in a new dictionary with the user ID as the key and the confirmation rate as the value.

  5. Return the confirmation rate dictionary.

Here's the Python code for the solution:

def getConfirmationRate(orders):

# Create dictionary to store user data
userData = {}

# Populate dictionary with order data
for order in orders:
    userid, orderid, action, timestamp = order
    if userid in userData:
        userData[userid].append((orderid, action, timestamp))
        userData[userid] = [(orderid, action, timestamp)]

# Calculate confirmation rate for each user
confirmationRates = {}

for userid, data in userData.items():
    confirmed_orders = 0
    total_orders = 0
    for order in data:
        if order[1] == "confirmed":
            confirmed_orders += 1
        total_orders += 1
    confirmationRate = round((confirmed_orders/total_orders)*100, 2)
    confirmationRates[userid] = confirmationRate

return confirmationRates

The time complexity of this solution is O(n), where n is the number of order attempts. The space complexity is also O(n), as we are storing all of the order attempts in the dictionary.

Confirmation Rate Solution Code