Google Interview Question for Software Engineer / Developers

• 0

Country: Berling
Interview Type: Phone Interview

Comment hidden because of low score. Click to expand.
0
of 0 vote

The idea is to first find all the bin cells, and add then to a queue. You can then do a flood fill for every iteration through the queue where you add the neighbours of each cell to said queue as you go. See Python implementation below (assumes by 'distance' you meant manhattan distance):

from collections import deque

def get_dist_to_bin(M: list[list[int]]) -> D list[list[int]]:
r, c = len(M), len(M)  # row, cols
bins = deque()

for i in range(r):
for j in range(c):
if M[i][j]:
continue

if    i and M[i-1][j]: dq.append( (i,j) )
elif j and M[i][j-1]: dq.append( (i,j) )
elif i<r-1 and M[i+1][j]: dq.append( (i,j) )
elif j<c-1 and M[i][j+1]: dq.append( (i,j) )

D = [*c for _ in range(r)]  #initialize output

curr_dist = 0
curr_cnt = len(bins)
while bins:
i, j = dq.popleft()
D[i, j] = curr_dist
curr_cnt  -= 1

if    i and not D[i-1][j]: dq.append( (i-1,j) )
elif j and not D[i][j-1]: dq.append( (i,j-1) )
elif i<r-1 and not D[i+1][j]: dq.append( (i+1,j) )
elif j<c-1 and not D[i][j+1]: dq.append( (i,j+1) )

if not curr_cnt: #finishes layer
curr_dist += 1
curr_cnt = len(bins)

return D

Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.