← Meta Interview Insights

Meta·Software Engineer·Technical Phone Screen·Intermediate

IntermediatePrefer not to say
May 2026Remote

Summary

Meta SWE coding round with an interactive grid search problem. The twist is you can only probe the grid through a black-box API, so you have to think carefully about how many calls you're burning.

Questions Asked (1)

Q1

Given an N×N grid hiding a single length-3 battleship (placed horizontally or vertically), find all three occupied cells. You can only interact with the grid through a bomb_location(x, y) API that returns true on a hit and false on a miss. Implement find_battleship(N) and discuss worst-case API call complexity.

Algorithms & Data StructuresAPI & IntegrationsTechnical Trade-offs
Author's notes

My first instinct was to just scan every cell row by row which is O(N^2) calls and technically correct but they pushed back immediately asking if I could do better.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Use a parity-based search to reduce the number of API calls: bomb every other cell to find a hit, then determine the ship's orientation and exact position with a few additional calls. Discuss the worst-case complexity, which is O(N^2) but with a constant factor of about 1/2, and mention that it's optimal in the worst case.

Pro tip: Mention that the parity strategy is optimal in the worst case because an adversary can place the ship to avoid all parity cells, but you can still guarantee finding it with at most ceil(N^2/2) + O(1) calls. Also, clarify that the API call complexity is measured in terms of the number of bomb_location calls.

1. Clarify the problem and constraints

Confirm that the ship is exactly length 3, placed horizontally or vertically, and that the grid is N×N. Ask if the ship can be at the edges and if the API returns true only for occupied cells.

2. Design a parity-based search strategy

Bomb all cells with (x+y) even (or odd) to find at least one hit. Since the ship occupies 3 consecutive cells, it must contain at least one cell of each parity, so this guarantees a hit.

3. Determine orientation and exact cells

Once a hit is found, check adjacent cells to determine if the ship is horizontal or vertical, then bomb the remaining cells along that line to identify all three occupied cells.

4. Analyze worst-case API call complexity

Count the maximum number of bomb_location calls: about N^2/2 for the parity search plus at most 4 additional calls to confirm orientation and position. Conclude that the worst-case complexity is O(N^2) with a constant factor of 1/2.

5. Discuss optimality and trade-offs

Explain that any algorithm must make at least ceil(N^2/2) calls in the worst case because the adversary can place the ship to avoid all cells of one parity, so the parity strategy is asymptotically optimal.

Key Points to Mention

  • Parity argument: any length-3 ship covers at least one cell of each parity, so bombing all cells of one parity guarantees a hit.
  • Worst-case API call complexity: O(N^2) with a constant factor of about 1/2, which is optimal.
  • Handling edge cases: ship at boundaries, N < 3 (impossible), and ensuring no out-of-bounds calls.
  • Efficiency: minimize API calls by avoiding redundant checks and using the hit to infer orientation.
  • Clear code structure: separate the search phase from the orientation/confirmation phase.
  • Communication: explain the strategy step-by-step and justify why it's optimal.

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