Problem · Array

Plan a Minimum-Cost Round Trip

Learn this problem
MediumMetaFULLTIMEONSITE INTERVIEW
See Meta hiring insights

Problem statement

You are given two cost arrays, departing and returning. A departure at index i costs departing[i], and a return at index j costs returning[j].

Choose zero-based indices i and j such that i < j and departing[i] + returning[j] is minimized. Return the two selected indices as [i, j].

If multiple pairs have the same minimum cost, choose the earliest departure index. If there is still a tie, choose the latest return index.

The requested time complexity is O(N).

What the interview report shared

The report gave the optimization goal, index-ordering rule, tie-breaking rule, target complexity, and one concrete example. This practice version is an estimated 90% match to that core task; the report did not include numeric limits, say whether the arrays always have equal length, or provide the original method and parameter names, so those details are not asserted here.

Function

planMinimumCostRoundTrip(departing: int[], returning: int[]) → int[]

Examples

Example 1

departing = [10,7,8,3,6]returning = [5,4,10,7,5]return = [3,4]

Choosing departure index 3 and return index 4 gives a total cost of departing[3] + returning[4] = 3 + 5 = 8, which is the minimum possible total.

More Meta problems

drafts saved locally
public int[] planMinimumCostRoundTrip(int[] departing, int[] returning) {
    // write your code here
}
departing[10,7,8,3,6]
returning[5,4,10,7,5]
expected[3,4]
checking account