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

0

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
Monotonic stack
Contribution technique
Combinatorics
Arrays
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
  • 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 Problems
Sample Answer
Requirements

Input: list of positive ints, n up to 10510^5. Output: ∑i≤jmin⁡(ai..aj) mod 109+7\sum_{i \le j} \min(a_i..a_j) \bmod 10^9+7. Enumerating ranges is O(n2)O(n^2), 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...


Submit Your Answer
Markdown supported