Finish One Land Ride and One Water Ride

Quick Overview

Choose one land ride and one water ride to finish as early as possible, respecting opening times, durations, waiting, and either category order.

Finish One Land Ride and One Water Ride

Company: Amazon

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

# Finish One Land Ride and One Water Ride A visitor must take exactly one land ride and exactly one water ride. The two rides may be taken in either order, one after the other. For each land ride `i`, `landStartTime[i]` is its earliest boarding time and `landDuration[i]` is its duration. The water rides are described by `waterStartTime[j]` and `waterDuration[j]` in the same way. A ride may begin at its opening time or any later time. Starting at time `t` means finishing at `t + duration`. Once the first ride ends, the visitor may immediately board the other ride if it is open, or wait until it opens. Return the earliest time at which both rides can be finished. ### C++ Interface ```cpp class Solution { public: int earliestFinishTime(vector<int>& landStartTime, vector<int>& landDuration, vector<int>& waterStartTime, vector<int>& waterDuration) { } }; ``` ### Examples **Example 1** **Input:** `landStartTime = [3, 10], landDuration = [5, 2], waterStartTime = [7, 1], waterDuration = [4, 8]` **Output:** `12` **Explanation:** Take land ride `0` from time `3` to `8`, then water ride `0` from `8` to `12`. Another equally early plan takes water ride `1` from `1` to `9`, waits until `10`, and takes land ride `1` until `12`. No legal choice finishes earlier. **Example 2** **Input:** `landStartTime = [9], landDuration = [2], waterStartTime = [2], waterDuration = [3]` **Output:** `11` **Explanation:** Take the water ride from `2` to `5`, wait until `9`, then take the land ride until `11`. Taking land first finishes both rides at `14`. ### Constraints - `1 <= n, m <= 100` - `landStartTime.length == landDuration.length == n` - `waterStartTime.length == waterDuration.length == m` - `1 <= landStartTime[i], landDuration[i], waterStartTime[j], waterDuration[j] <= 1000` ### Clarifying Questions - Are opening times fixed appointments? No; boarding at any later time is allowed. - Can the rides overlap? No. The second ride starts only after the first one finishes. - Must every ride be taken? No; choose exactly one from each category. There is no additional travel delay in this task. ### What a Strong Answer Covers - A finish time supported by a valid choice of one ride from each category. - Correct accounting for opening times, durations, and any waiting. - The one-ride-per-category case, tied finish times, and a justified complexity bound. ### Follow-up Questions - What extra rule would be needed if the API also returned ride indices when several plans finish at the same time? - How would the problem statement change if the visitor had a specified earliest arrival time? ```hint The small input bounds There are at most 100 rides in each category. For any selected pair, compare the finish times of its two permitted orders, including waiting before the second ride. ```

Overview: Choose one land ride and one water ride to finish as early as possible, respecting opening times, durations, waiting, and either category order.

Amazon logo
Amazon
Apr 30, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Finish One Land Ride and One Water Ride

A visitor must take exactly one land ride and exactly one water ride. The two rides may be taken in either order, one after the other.

For each land ride i, landStartTime[i] is its earliest boarding time and landDuration[i] is its duration. The water rides are described by waterStartTime[j] and waterDuration[j] in the same way.

A ride may begin at its opening time or any later time. Starting at time t means finishing at t + duration. Once the first ride ends, the visitor may immediately board the other ride if it is open, or wait until it opens.

Return the earliest time at which both rides can be finished.

C++ Interface

class Solution {
public:
    int earliestFinishTime(vector<int>& landStartTime, vector<int>& landDuration, vector<int>& waterStartTime, vector<int>& waterDuration) {
        
    }
};

Examples

Example 1

Input: landStartTime = [3, 10], landDuration = [5, 2], waterStartTime = [7, 1], waterDuration = [4, 8]

Output: 12

Explanation: Take land ride 0 from time 3 to 8, then water ride 0 from 8 to 12. Another equally early plan takes water ride 1 from 1 to 9, waits until 10, and takes land ride 1 until 12. No legal choice finishes earlier.

Example 2

Input: landStartTime = [9], landDuration = [2], waterStartTime = [2], waterDuration = [3]

Output: 11

Explanation: Take the water ride from 2 to 5, wait until 9, then take the land ride until 11. Taking land first finishes both rides at 14.

Constraints

  • 1 <= n, m <= 100
  • landStartTime.length == landDuration.length == n
  • waterStartTime.length == waterDuration.length == m
  • 1 <= landStartTime[i], landDuration[i], waterStartTime[j], waterDuration[j] <= 1000

Clarifying Questions Guidance

  • Are opening times fixed appointments? No; boarding at any later time is allowed.
  • Can the rides overlap? No. The second ride starts only after the first one finishes.
  • Must every ride be taken? No; choose exactly one from each category. There is no additional travel delay in this task.

What a Strong Answer Covers Guidance

  • A finish time supported by a valid choice of one ride from each category.
  • Correct accounting for opening times, durations, and any waiting.
  • The one-ride-per-category case, tied finish times, and a justified complexity bound.

Follow-up Questions Guidance

  • What extra rule would be needed if the API also returned ride indices when several plans finish at the same time?
  • How would the problem statement change if the visitor had a specified earliest arrival time?