Count Later Trips With a Lower Fare for Each Trip
Last updated: November 29, 2026
Quick Overview
Given trip fares in chronological order, return an array where answer[i] is the number of trips after i whose fare is strictly lower than fare[i].
Uber
November 29, 20260
0
0 solved
Given trip fares in chronological order, return an array where answer[i] is the number of trips after i whose fare is strictly lower than fare[i].
An Uber onsite question that checks whether you know an ordered structure supporting rank queries, such as a Fenwick tree over compressed values. It mirrors pricing analyses that ask how often a quote was undercut later.
What the Interviewer Expects
- Rejects the O(n^2) double loop for large n
- Processes from right to left and queries how many smaller values were seen
- Uses coordinate compression to map fares to dense ranks
- Implements a Fenwick tree correctly with 1-based indices
- Mentions merge sort counting as an alternative
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
- Count later trips with a fare within a range [a, b] instead.
- How would you handle a sliding window of the next W trips only?
- Solve it with a modified merge sort and compare constants.
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: list of integer fares, up to around .
- Output: list of counts of strictly smaller fares to the right.
- Fares may repeat and may be large or negative....
High-Level Design
Scan from right to left. Keep a frequency structure over fare values seen so far (all to the right). For the current fare, the answer is how many seen values are strictly less, which is a prefix sum o...