Top Three Earning Drivers per City Each Week

Last updated: November 29, 2026

Quick Overview

Given a trips table, return the top three drivers by total completed-trip earnings in each city for each week, keeping ties.

Uber
Data Manipulation (SQL/Python)
Data Scientist
Uber
November 29, 2026
Data Scientist
Technical Screen
Data Manipulation (SQL/Python)
Easy

0

0

0 solved


Given a trips table, return the top three drivers by total completed-trip earnings in each city for each week, keeping ties.

Driver earnings by city and week is a bread-and-butter Uber analytics query used in ops reviews and incentive targeting. The interviewer checks aggregation before ranking, the right ranking function for ties, and filtering on trip status.

What the Interviewer Expects
  • Filter to completed trips before aggregating
  • Aggregate to driver-city-week before applying the window
  • Choose DENSE_RANK, RANK or ROW_NUMBER deliberately
  • Partition the window by city and week
  • Explain how ties change the output size
Key Topics to Cover
window functions
DENSE_RANK
aggregation
partitioning
How to Approach This
  1. Clarify the schema and expected output format before writing queries.
  2. Use CTEs (WITH clauses) to break complex queries into readable steps.
  3. Consider window functions (ROW_NUMBER, RANK, LAG, LEAD) for ranking and sequential analysis.
  4. Watch for NULLs, duplicates, and edge cases in JOINs and GROUP BY.
  5. For pandas, prefer vectorized operations over row-by-row iteration.
Possible Follow-up Questions
  • How would you return exactly three rows per city-week?
  • How would you include tips and incentives stored in other tables?
  • How would you handle drivers who operate in two cities in the same week?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice SQL Problems
Sample Answer
Clarify the Data

trips(trip_id, driver_id, city_id, completed_at, fare_usd, status). Ask: is fare_usd the driver's earnings or the rider's total (they differ by service fee)? Which time zone defines the week (use ...

Approach
  1. Filter status = 'completed'.
  2. Aggregate per city, week and driver.
  3. Rank within each (city, week) by earnings descending with DENSE_RANK, so tied drivers share a rank.
  4. Keep rank at most ...

Submit Your Answer
Markdown supported