← Hive.ai Interview Insights

Hive.ai·Software Engineer·Technical Phone Screen·Intermediate

Intermediate
Apr 2026

Summary

Coding round at Hive.ai for a software engineer role. One grid problem, felt straightforward once I remembered the BFS angle, but I definitely fumbled around for a bit before getting there.

Questions Asked (1)

Q1

Given an n x n binary grid where 0 is water and 1 is land, find the water cell with the maximum Manhattan distance to its nearest land cell and return that distance. Return -1 if no land or no water exists.

Algorithms & Data Structures
Author's notes

I started thinking about running BFS from each water cell individually which would have been way too slow.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Use multi-source BFS starting from all land cells simultaneously to compute the shortest distance to the nearest land for every water cell. Then return the maximum distance found, handling edge cases where there is no land or no water by returning -1.

Pro tip: Mention that multi-source BFS is optimal because it processes each cell once, achieving O(n^2) time, and explicitly discuss edge cases like all land or all water to show thoroughness.

1. Clarify and Handle Edge Cases

Check if there is any land or any water in the grid. If either is missing, return -1 immediately.

2. Initialize BFS Queue and Distance Matrix

Create a queue and enqueue all land cells with distance 0. Initialize a distance matrix with -1 for unvisited cells.

3. Multi-Source BFS

While the queue is not empty, pop a cell and explore its four neighbors. If a neighbor is water and unvisited, set its distance to current distance + 1 and enqueue it.

4. Track Maximum Distance

During BFS, keep track of the maximum distance assigned to any water cell. After BFS, return this maximum.

5. Analyze Complexity

State that time complexity is O(n^2) since each cell is processed once, and space complexity is O(n^2) for the queue and distance matrix.

Key Points to Mention

  • Multi-source BFS efficiently computes distances from all land cells simultaneously.
  • Edge cases: no land or no water should return -1.
  • Time and space complexity: O(n^2) time, O(n^2) space.
  • Alternative approaches like dynamic programming or repeated BFS are less efficient.
  • Manhattan distance is computed as |r1 - r2| + |c1 - c2|.
  • The maximum distance is the answer, not the sum or average.

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