Trips and Users LeetCode Solution

Last updated on October 9th, 2024 at 09:59 pm

This Leetcode problem Trips and Users LeetCode Solution is done in SQL.

List of all LeetCode Solution

Level of Question

Hard

Trips and Users LeetCode Solution

Trips and Users LeetCode Solution

Problem Statement

Column NameType
idint
client_idint
driver_idint
city_idint
statusenum
request_atdate
Table: Trips

id is the primary key (column with unique values) for this table. The table holds all taxi trips. Each trip has a unique id, while client_id and driver_id are foreign keys to the users_id at the Users table. Status is an ENUM (category) type of (‘completed’, ‘cancelled_by_driver’, ‘cancelled_by_client’).

Column NameType
users_idint
bannedenum
roleenum
Table: Users

users_id is the primary key (column with unique values) for this table. The table holds all users. Each user has a unique users_id, and role is an ENUM type of (‘client’, ‘driver’, ‘partner’). banned is an ENUM (category) type of (‘Yes’, ‘No’).

The cancellation rate is computed by dividing the number of canceled (by client or driver) requests with unbanned users by the total number of requests with unbanned users on that day.

Write a solution to find the cancellation rate of requests with unbanned users (both client and driver must not be banned) each day between "2013-10-01" and "2013-10-03". Round Cancellation Rate to two decimal points.

Return the result table in any order.

The result format is in the following example.

Example 1:
Input:

users_id banned role
1Noclient
2Yesclient
3Noclient
4Noclient
10Nodriver
11Nodriver
12Nodriver
13Nodriver
Users table:
idclient_id driver_idcity_id statusrequest_at
11101completed 2013-10-01
22111cancelled_by_driver 2013-10-01
33126completed 2013-10-01
44136cancelled_by_driver 2013-10-01
51101completed2013-10-02
62116completed2013-10-02
73126completed2013-10-02
821212completed2013-10-03
931012completed2013-10-03
1041313cancelled_by_driver2013-10-03
Trips table:

Output:

Day Cancellation Rate
2013-10-010.33
2013-10-020.00
2013-10-030.50

Explanation:
On 2013-10-01:
– There were 4 requests in total, 2 of which were canceled.
– However, the request with Id=2 was made by a banned client (User_Id=2), so it is ignored in the calculation.
– Hence there are 3 unbanned requests in total, 1 of which was canceled.
– The Cancellation Rate is (1 / 3) = 0.33
On 2013-10-02:
– There were 3 requests in total, 0 of which were canceled.
– The request with Id=6 was made by a banned client, so it is ignored.
– Hence there are 2 unbanned requests in total, 0 of which were canceled.
– The Cancellation Rate is (0 / 2) = 0.00
On 2013-10-03:
– There were 3 requests in total, 1 of which was canceled.
– The request with Id=8 was made by a banned client, so it is ignored.
– Hence there are 2 unbanned request in total, 1 of which were canceled.
– The Cancellation Rate is (1 / 2) = 0.50

1. Trips and Users LeetCode Solution MySQL

with stats as (
  select 
    Request_at, 
    T.Status <> 'completed' as IsCancelled
  from Trips T 
  join Users C on (Client_Id = C.Users_Id and C.Banned = 'No') 
  join Users D on (Driver_Id = D.Users_Id and D.Banned = 'No') 
  where
    Request_at between '2013-10-01' and '2013-10-03'
)
select 
  Request_at as Day,
  Round(
    cast(sum(IsCancelled) as real) / cast(count(*) as real),
    2
  ) as 'Cancellation Rate'
from stats
group by Request_at
;

2. Trips and Users LeetCode Solution Pandas

import pandas as pd

def trips_and_users(trips: pd.DataFrame, users: pd.DataFrame) -> pd.DataFrame:

    users = users[users.banned == 'No']
    trips = trips[trips.request_at
                     .between('2013-10-01','2013-10-03')
                    ].rename(columns = {'request_at': 'Day'})

    trips['can'] = trips.status.str.startswith('can')

    trips = trips[(trips.client_id.isin(users.users_id)) &
                  (trips.driver_id.isin(users.users_id))
                  ].groupby('Day')['can'].agg(['sum','size']).reset_index()

    trips['Cancellation Rate'] = (trips['sum']/trips['size']).round(2)
    
    return trips[['Day','Cancellation Rate']]
Scroll to Top