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
Coding & Algorithms
Software Engineer
Uber
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Hard

0

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
Fenwick tree
coordinate compression
inversion counting
ordered statistics
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
  • 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 Problems
Sample Answer
Requirements
  • Input: list of integer fares, up to around 10510^5.
  • 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...


Submit Your Answer
Markdown supported