My first instinct was brute force, O(n^2) nested loops, which obviously works but dies on n=100000.
Clarify the problem constraints and edge cases, then propose an O(n) solution using a running minimum of outbound prices. Iterate through days, maintaining the minimum outbound price seen so far, and for each day compute the total cost with the return price on that day, updating the global minimum.
Pro tip: Always discuss trade-offs between brute force and optimized solutions, and mention that you would test with edge cases like no valid pair or large inputs. This shows you consider both correctness and efficiency, which is crucial at Meta.
Restate the problem in your own words and ask clarifying questions about input constraints, array sizes, and whether prices can be negative or zero.
Mention that a naive O(n^2) solution checks all pairs (i, j) with i < j, but it's inefficient for large n.
Explain that you can iterate through the days once, keeping track of the minimum outbound price seen so far, and for each day compute the total cost with the return price on that day.
Use a small example to demonstrate how the algorithm works, showing the running minimum and the updated minimum total cost.
State that the time complexity is O(n) and space is O(1). Discuss edge cases like when no valid pair exists (return -1) and arrays of length 1.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.