Google Interview Question for Software Engineer / Developers


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[0])  # 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 = [[0]*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

- Anonymous December 03, 2021 | Flag Reply


Add a Comment
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.

Learn More

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.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More