[Rejected] Facebook virtual onsite for Sr Software Engineer
Anonymous User
1901

Following question was asked in first round of coding interview.

A 2D grid represent height of land. Find out max height of water to travel from S to E in 2d grid. You can travel up, down, bottom and left direction. You can't jump to a cell if it is submerged with water. 
S 3 5 1 5
4 5 1 4 6
3 4 5 6 5
4 6 7 8 E

I came up with following approach and wote the code.

  1. Apply binary search on low and max land height
  2. mid value in binary search will represent water height
  3. Use dfs to find out if path is possible with mid as water height
  4. If yes, then update global ans as max value and move right to find next higher value
  5. If no then move left to try smaller water height value

Edit: I found exactly similar problem in leetcode. Binary search soln I proposed did work with little modification.
https://leetcode.com/problems/path-with-maximum-minimum-value/

Later I was reading network flow algorithm and realized that this can also be solved applying Ford Fulkerson Edmond Karp algorithm (there is alternate algo as well to use dfs with binary search)

  1. Given S is source
  2. Given E is target i.e. sink
  3. Use cell value as capacity of pipe (i.e. edge)
  4. Convert 2D graph to 1D graph i.e. each cell will represent as 0....m*n-1 nodes
  5. Use adjacency matrix to store residual capacity from u->v also use v->u
  6. Start while loop
  7. Run bfs to find out shortest path from start to target
  8. if no path exist then break
  9. if exists then get the minimum capacity
  10. Update flow with new minimum capacity
  11. update residual capacity for the minimum capacity found
  12. Return the flow as final answer

Thoughts?

Comments (6)