Sum of the Cheapest Fare Over Every Contiguous Trip Range
Last updated: November 29, 2026
Quick Overview
Given a list of fares for consecutive trips, compute the sum of the minimum fare over every contiguous range of trips, modulo 1,000,000,007, as an input to a fare-floor analysis.
Uber
November 29, 20260
0
0 solved
Given a list of fares for consecutive trips, compute the sum of the minimum fare over every contiguous range of trips, modulo 1,000,000,007, as an input to a fare-floor analysis.
A contribution-counting problem in the style of Uber's onsite coding round. The interviewer wants you to flip the question from 'minimum of each range' to 'how many ranges does each fare win', computed with previous-less and next-less boundaries from monotonic stacks.
What the Interviewer Expects
- Rejects the O(n^2) enumeration of ranges
- Counts, for each fare, the ranges where it is the minimum
- Finds previous-smaller and next-smaller boundaries with a monotonic stack
- Breaks ties asymmetrically so equal fares are not double counted
- Applies the modulus
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
- How would you compute the sum of range maximums instead?
- How would you get sum of (max minus min) over all ranges?
- How does the tie rule change if you flip strict and non-strict comparisons?
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 positive ints, n up to . Output: . Enumerating ranges is , too slow....
High-Level Design
Fare a[i] is the minimum of a range [l, r] when l is after the previous index with a smaller or equal value and r is before the next index with a strictly smaller value. If left[i] counts ch...