LeetCode 262 - Trips & Users (Python and SQL) [Hard]

Pubblicato il: 31 marzo 2024
sul canale di: Ryan & Matt Data Science
468
9

🧠 Don’t miss out! Get FREE access to my Skool community — packed with resources, tools, and support to help you with Data, Machine Learning, and AI Automations! 📈 https://www.skool.com/data-and-ai-aut...

In this advanced tutorial, we solve LeetCode 262: Trips and Users using both SQL and Python (Pandas). This problem tests your ability to handle multi-table joins, complex filtering, and grouped aggregates with conditions—perfect for real-world data scenarios like ride-sharing platforms.


🚀 Hire me for Data Work: https://ryanandmattdatascience.com/da...
👨‍💻 Mentorships: https://ryanandmattdatascience.com/me...
📧 Email: ryannolandata@gmail.com
🌐 Website & Blog: https://ryanandmattdatascience.com/
🖥️ Discord:   / discord  
📚 *Practice SQL & Python Interview Questions: https://stratascratch.com/?via=ryan
📖 *SQL and Python Courses: https://datacamp.pxf.io/XYD7Qg

🍿 WATCH NEXT
LeetCode Playlist:    • Leetcode Data Science Interview Questions  
LeetCode 184:    • LeetCode 184 – Department Highest Salary (...  
LeetCode 577:    • LeetCode 577 -  Employee Bonus (Python and...  
LeetCode 175:    • Leetcode 175 Combine Two Tables (Python an...  

In this video, I tackle LeetCode 262, "Trips and Users," a hard-level SQL and Python problem that requires calculating cancellation rates for ride-sharing requests. We need to find the cancellation rate of requests with unbanned users for each day between October 1st and October 3rd, 2013, rounded to two decimal places.

The problem involves working with two tables: a trips table containing ride information (ID, client ID, driver ID, city ID, status, and request date) and a users table with user details (user ID, banned status, and role). The challenge is determining which trips were cancelled while filtering out any rides involving banned users—either as clients or drivers.

I walk through my solution using CTEs in SQL, creating separate queries for total rides and cancelled rides, then joining them together. The key complexity comes from needing to join the users table twice: once for clients and once for drivers. In the Python pandas solution, I demonstrate multiple merge operations with custom suffixes to track client and driver information separately.

By the end of this tutorial, you'll understand how to handle complex multi-table joins, work with date filtering, calculate rates with proper null handling, and format results to specific decimal places in both SQL and Python.

TIMESTAMPS
00:00 Problem Introduction & Overview
01:42 Understanding the Tables & Data Structure
03:02 SQL Solution - Setting Up CTEs
05:17 Building the Total Rides Query
07:32 Creating the Cancelled Rides Query
09:53 Joining CTEs & Final Calculation
12:40 Python Pandas Solution Begins
15:17 Filtering for Banned Users & Date Range
18:15 Calculating All Rides Count
20:17 Merging Data & Computing Cancellation Rate
23:00 Debugging & Final Solution
25:20 Recap & Channel Information

OTHER SOCIALS:
Ryan’s LinkedIn:   / ryan-p-nolan  
Matt’s LinkedIn:   / matt-payne-ceo  
Twitter/X: https://x.com/RyanMattDS

Who is Ryan
Ryan is a Data Scientist at a fintech company, where he focuses on fraud prevention in underwriting and risk. Before that, he worked as a Data Analyst at a tax software company. He holds a degree in Electrical Engineering from UCF.

Who is Matt
Matt is the founder of Width.ai, an AI and Machine Learning agency. Before starting his own company, he was a Machine Learning Engineer at Capital One.

*This is an affiliate program. We receive a small portion of the final sale at no extra cost to you.


In questa pagina del sito puoi guardare il video online LeetCode 262 - Trips & Users (Python and SQL) [Hard] della durata di ore minuti seconda in buona qualità , che l'utente ha caricato Ryan & Matt Data Science 31 marzo 2024, condividi il link con amici e conoscenti, su youtube questo video è già stato visto 468 volte e gli è piaciuto 9 spettatori. Buona visione!