Pair Waiting Riders With Drivers on a Highway to Minimize Distance

Last updated: November 29, 2026

Quick Overview

There are n riders and n idle drivers at integer mile markers along a single highway. Pair every rider with a distinct driver to minimize the total pickup distance, and return that minimum total.

Uber
Coding & Algorithms
Software Engineer
Uber
November 29, 2026
Software Engineer
Phone Screen
Coding & Algorithms
Easy

0

0

0 solved


There are n riders and n idle drivers at integer mile markers along a single highway. Pair every rider with a distinct driver to minimize the total pickup distance, and return that minimum total.

A simplified one-dimensional version of Uber's matching problem, used as a phone screen warm-up. The interviewer wants the sort-and-pair insight plus a short argument that crossing pairs can always be uncrossed.

What the Interviewer Expects
  • Sorts riders and drivers independently
  • Pairs the i-th rider with the i-th driver
  • Argues why crossing assignments are never better
  • Notes that greedy nearest-first per rider can be suboptimal
  • Handles duplicates and n = 0
Key Topics to Cover
Greedy
Sorting
Matching
Exchange argument
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • What if there are more drivers than riders?
  • What changes in two dimensions?
  • How would you return the actual pairing with original indices?
Sharpen Your Skills on Codemia

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

Practice DSA Problems
Sample Answer
Requirements

Input: two lists of equal length n of integers. Output: minimum sum of |rider - driver| over a perfect matching. n can be up to 10510^5, so Hungarian-style O(n3)O(n^3) is out....

High-Level Design

Sort both lists and pair by rank. Exchange argument: suppose riders r1 <= r2 are matched to drivers d2 >= d1 in crossed order. For points on a line, `|r1 - d1| + |r2 - d2| <= |r1 - d2| + |r2 - d1|...


Submit Your Answer
Markdown supported