Find the Cheapest Valid Round Trip

Quick Overview

Find the cheapest outbound and return fares subject to a minimum stay between the travel days. Use suffix minima to evaluate every valid departure in linear time, and discuss deterministic date selection and lower-space variants.

Find the Cheapest Valid Round Trip

Company: Uber

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Onsite

Implement `cheapest_round_trip(outbound, return_fares, min_gap)`. `outbound[i]` is the fare for departing on day `i`, and `return_fares[j]` is the fare for returning on day `j`. Choose one departure and one return such that `j - i >= min_gap`, and return the minimum total fare. The two fare arrays have the same length, `min_gap` is positive, and at least one valid pair exists. Target `O(n)` time and `O(n)` auxiliary space, or explain how to reduce the extra space while preserving linear time. ```hint Precompute future choices For each day, record the cheapest return fare available from that day through the end of the array. ``` ```hint Shift by the required stay When considering departure day `i`, combine its fare with the suffix minimum beginning at `i + min_gap`, not at `i`. ``` ### Discussion Extensions - How does the solution simplify when only the minimum total cost is required rather than the chosen dates? - How would you return deterministic dates when several pairs have the same minimum fare?

Quick Answer: Find the cheapest outbound and return fares subject to a minimum stay between the travel days. Use suffix minima to evaluate every valid departure in linear time, and discuss deterministic date selection and lower-space variants.

|Home/Coding & Algorithms/Uber
Uber logo
Uber
Aug 16, 2026, 12:00 AM
mediumSoftware EngineerOnsiteCoding & Algorithms
0
0

Implement cheapest_round_trip(outbound, return_fares, min_gap).

outbound[i] is the fare for departing on day i, and return_fares[j] is the fare for returning on day j. Choose one departure and one return such that j - i >= min_gap, and return the minimum total fare.

The two fare arrays have the same length, min_gap is positive, and at least one valid pair exists. Target O(n) time and O(n) auxiliary space, or explain how to reduce the extra space while preserving linear time.

Discussion Extensions

  • How does the solution simplify when only the minimum total cost is required rather than the chosen dates?
  • How would you return deterministic dates when several pairs have the same minimum fare?

Submit Your Answer to Earn 20XP

Sign in to leave a comment

Loading comments...