Generate Every Valid Stop Order for a Pooled Ride
Last updated: November 29, 2026
Quick Overview
A pooled ride has n riders, each with a pickup Pi and a drop-off Di. List every ordering of the 2n stops in which each rider's pickup comes before their drop-off, and give a formula for how many there are.
Uber
November 29, 20260
0
0 solved
A pooled ride has n riders, each with a pickup Pi and a drop-off Di. List every ordering of the 2n stops in which each rider's pickup comes before their drop-off, and give a formula for how many there are.
An Uber technical screen backtracking question inspired by UberX Share routing. Interviewers check that you prune invalid prefixes instead of filtering permutations, and that you can derive the count.
What the Interviewer Expects
- Backtracks by choosing either an unpicked rider's pickup or an onboard rider's drop-off
- Never generates an invalid prefix
- Tracks state with two sets or counters and undoes choices on return
- Derives the count (2n)! / 2^n
- Explains why full enumeration only works for small n
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- Add a seat capacity so at most c riders are onboard at once.
- Return only the ordering with the shortest total distance.
- Count valid orderings modulo 1e9+7 for n up to 500.
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
- Input: integer
n(small, up to about 5). - Output: list of orderings as lists of strings like
P1,D1. - Constraint:
PibeforeDifor every rider....
High-Level Design
Build the sequence one stop at a time. At each step there are two kinds of legal moves: pick up any rider not yet picked up, or drop off any rider currently onboard. Keep waiting and onboard sets....