← Meta Interview Insights

Meta·Software Engineer·Technical Phone Screen·Senior

Senior
May 2026

Summary

Meta Production Engineer interview with a coding round focused on a grid traversal problem. The follow-up discussion got surprisingly nitpicky about micro-optimizations, which I wasn't expecting at all.

Questions Asked (1)

Q1

Given a 2D board containing 'X' and '.', count the number of battleships. Battleships occupy 1×k or k×1 cells and never touch each other. Solve it in O(R·C) time with O(1) extra space.

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

The key insight is you only count a cell if its top neighbor and left neighbor are both not 'X', meaning it's the top-left corner of a ship.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Clarify the constraints and assumptions, then propose a single-pass solution that counts a battleship only when its top-left cell is encountered. Explain the O(1) space trick of using the board itself to mark visited cells, and discuss trade-offs with modifying input.

Pro tip: Mention that you can avoid modifying the input by checking only the top and left neighbors, which is sufficient because battleships are straight lines and non-touching. This demonstrates attention to immutability and edge cases.

1. Clarify constraints and assumptions

Confirm that battleships are 1×k or k×1, never touch, and that the board can be modified or not. Ask about input size and whether in-place modification is acceptable.

2. Identify the counting condition

A cell is the start of a battleship if it is 'X' and has no 'X' above or to the left. Count such cells to get the total number of battleships.

3. Design the algorithm

Iterate through each cell. If it's 'X' and (row==0 or board[row-1][col]=='.') and (col==0 or board[row][col-1]=='.'), increment count. This ensures each ship is counted exactly once.

4. Analyze complexity and space

Time is O(R·C) since each cell is visited once. Extra space is O(1) because only a counter is used; no additional data structures are needed.

5. Discuss trade-offs and edge cases

If modifying the board is allowed, you could mark visited cells to avoid re-scanning, but that still uses O(1) space. Handle empty board, single cell, and ships at edges.

Key Points to Mention

  • Battleships are straight lines (1×k or k×1) and never touch, so each ship has a unique top-left cell.
  • Counting only cells with no 'X' above or left correctly counts each ship once.
  • Time complexity O(R·C) and space complexity O(1) without modifying input.
  • Edge cases: empty board, ships at borders, single-cell ships.
  • Trade-off: modifying input to mark visited cells vs. using neighbor checks for immutability.
  • Potential follow-up: what if ships can touch? Then need different approach (e.g., DFS/BFS).

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