← Meta Interview Insights

Meta·Software Engineer·Onsite - Coding / Algorithms·Senior

SeniorPrefer not to say
May 2026

Summary

Meta onsite coding round, one question but it came with teeth. The base problem was straightforward enough but they pushed into follow-ups pretty fast and I wasn't as ready for the verbal explanation part as I thought I was.

Questions Asked (1)

Q1

Given an array of daily stock prices, find the maximum profit from a single buy and sell. Follow-up: extend your solution to handle unlimited transactions, then at most k transactions.

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

The base case I had no problem with, single pass tracking the running minimum and updating max profit as you go.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by clarifying the problem constraints and confirming edge cases, then present the optimal O(n) time, O(1) space solution for the single transaction case using a running minimum. For the follow-ups, build up from the single transaction to unlimited transactions (sum of positive differences) and finally to at most k transactions using dynamic programming, explaining the state transitions and space optimization.

Pro tip: Explicitly discuss the trade-offs between the DP approaches for k transactions (e.g., O(kn) time vs. O(n) time when k >= n/2) and mention that the unlimited case is a special case of k >= n/2. This shows you understand both the problem and practical optimization.

1. Clarify requirements and edge cases

Ask if the array can be empty, if prices are integers, and if multiple transactions are allowed in the follow-ups. Confirm that you cannot sell before buying and that you want to maximize profit.

2. Solve single transaction optimally

Explain the one-pass algorithm: track the minimum price seen so far and compute the maximum profit as the difference between the current price and that minimum. This gives O(n) time and O(1) space.

3. Extend to unlimited transactions

For unlimited transactions, the maximum profit is the sum of all positive differences between consecutive days. This is because you can capture every upward movement.

4. Generalize to at most k transactions

Use dynamic programming with states: dp[i][j] = maximum profit up to day i with at most j transactions. Optimize to O(kn) time and O(k) space by iterating over transactions and days, or use the O(n) approach when k >= n/2.

5. Analyze complexity and trade-offs

Discuss time and space complexities for each solution, and explain why the DP is necessary for general k. Mention that the unlimited case is a special case of k >= n/2 and can be solved in O(n) time.

Key Points to Mention

  • Single transaction: one-pass algorithm with running minimum, O(n) time and O(1) space.
  • Unlimited transactions: sum of positive differences between consecutive days, O(n) time.
  • At most k transactions: dynamic programming with state dp[i][j] representing max profit up to day i with at most j transactions.
  • Space optimization: reduce DP to O(k) space by using two arrays or variables.
  • Special case: when k >= n/2, the problem reduces to unlimited transactions and can be solved in O(n) time.
  • Edge cases: empty array, decreasing prices, k=0, and large k.

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