Battleships in a Board

Algorithms
Medium
Uber
122.4K views

Given an $m \times n$ board where 'X's represent battleships and '.'s represent water, count the number of battleships. Battleships are placed horizontally or vertically, and are separated by water. Solve in $O(n)$ without modifying the board.

Why Interviewers Ask This

Uber interviewers use this problem to assess a candidate's ability to optimize for space complexity while maintaining algorithmic clarity. They specifically evaluate if you can identify that battleships are contiguous components and count only their unique starting points, rather than using BFS or DFS which would require O(m*n) extra space. This tests your understanding of greedy strategies and single-pass iteration constraints.

How to Answer This Question

1. Clarify Constraints: Immediately confirm the rules regarding horizontal vs. vertical placement and the prohibition on modifying the input board. Mention Uber's focus on efficiency by stating you aim for O(1) space. 2. Identify the Core Logic: Explain that counting every 'X' will overcount ships. Instead, propose counting only the top-leftmost cell of each ship. A cell is a start if it is an 'X' and has no 'X' immediately above it and no 'X' immediately to its left. 3. Define the Algorithm: Describe a nested loop iterating through rows and columns. For each cell, apply the 'start' condition check before incrementing the counter. 4. Handle Edge Cases: Briefly mention checking boundary conditions where accessing row-1 or col-1 might be out of bounds. 5. Complexity Analysis: Conclude by explicitly stating the time complexity is O(m*n) and space complexity is O(1), satisfying the strict requirements without auxiliary data structures.

Key Points to Cover

  • Recognizing that counting ship starts avoids overcounting contiguous segments
  • Explicitly addressing the O(1) space constraint by rejecting BFS/DFS
  • Defining the precise condition for a cell being a ship's head
  • Handling boundary checks for the top row and leftmost column safely
  • Demonstrating clear communication of time and space complexity trade-offs

Sample Answer

To solve this efficiently within Uber's high-performance standards, I would avoid standard graph traversal algorithms like BFS or DFS because they typically require a visited array or recursion stack, violating the O(1)…

Common Mistakes to Avoid

  • Using a visited array or recursion stack which violates the O(1) space requirement
  • Iterating through the board twice or using complex data structures unnecessarily
  • Failing to check both the top and left neighbors, leading to incorrect counts
  • Attempting to mark visited cells by modifying the input board directly

Sound confident on this question in 5 minutes

Answer once and get a 30-second AI critique of your structure, content, and delivery. First attempt is free — no signup needed.

Try it free

Related Interview Questions

Browse all 145 Algorithms questionsBrowse all 57 Uber questions