← Meta Interview Insights

Meta·Software Engineer·Technical Phone Screen·Intermediate

IntermediatePrefer not to say
Jun 2026Remote

Summary

Meta SWE coding round, one algorithm question that looked simple on the surface but they wanted the full treatment: proof of correctness, complexity analysis, and a follow-up about returning the actual indices.

Questions Asked (1)

Q1

Given an array of daily stock prices, find the maximum profit from at most one buy-sell transaction where the buy must occur before the sell. Return 0 if no profit is possible. Solve it in O(n) time and O(1) space, explain your algorithm, prove its correctness, and analyze the complexity. Bonus: return the actual buy and sell day indices.

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

I knew this problem cold, or thought I did.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Clarify the problem constraints and edge cases, then present the single-pass greedy algorithm that tracks the minimum price seen so far and the maximum profit. Explain the algorithm step-by-step, prove its correctness using an invariant, and analyze time and space complexity. If time permits, extend to return the buy and sell indices.

Pro tip: Emphasize that the algorithm is optimal and handles all edge cases, including decreasing prices and single-element arrays. Mention that the same pattern applies to similar problems like 'Best Time to Buy and Sell Stock II' but with modifications.

1. Clarify requirements and edge cases

Confirm that the array represents daily prices, buy must precede sell, and only one transaction is allowed. Discuss edge cases: empty array, single day, strictly decreasing prices, and all equal prices.

2. Present the algorithm

Describe the single-pass approach: initialize min_price to infinity and max_profit to 0. Iterate through prices, update min_price if current price is lower, else compute profit if sold today and update max_profit if higher.

3. Prove correctness

Use an invariant: after processing day i, min_price is the minimum price in days 0..i, and max_profit is the maximum profit achievable with a transaction ending by day i. Show that the algorithm maintains this invariant and yields the correct result.

4. Analyze complexity

State that the algorithm runs in O(n) time because it makes a single pass, and O(1) space because it uses only a few variables. Compare with brute-force O(n^2) to highlight efficiency.

5. Bonus: track indices

Modify the algorithm to record the buy day when updating min_price and the sell day when updating max_profit. Return the indices along with the profit.

Key Points to Mention

  • Single-pass greedy approach with O(n) time and O(1) space.
  • Maintaining the minimum price seen so far and the maximum profit.
  • Proof of correctness using loop invariant.
  • Handling edge cases: empty array, single element, no profit.
  • Comparison with brute-force O(n^2) solution.
  • Extension to return buy and sell indices.

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.