Google Interview Question for Software Engineers


Country: Korea
Interview Type: In-Person




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

Looking for interview experience sharing and coaching?

Visit AONECODE.COM for ONE-ON-ONE private lessons by FB, Google and Uber engineers!

SYSTEM DESIGN Courses (highly recommended for candidates of FB, LinkedIn, Amazon, Google & Uber etc.)
ALGORITHMS (conquer DP, Greedy, Graph, Advanced Algorithms & Clean Coding),
latest interview questions sorted by companies,
mock interviews.

Our students got hired from G, U, FB, Amazon, LinkedIn, MS and other top-tier companies after weeks of training.

Email us aonecoding@gmail.com with any questions. Thanks!


SOLUTION:
BFS

from heapq import heappush, heappop
import numpy as np
def minCostPath(maze):
    if len(maze) is 0 or len(maze[0]) == 0 or maze[0, 0] == -1:
        return
    
    m, n = len(maze), len(maze[0])
    distances = np.zeros(shape = (m, n))
    distances.fill(-1)
    
    heap = []
    heappush(heap, (maze[0, 0], 0, 0))
    value = maze[0, 0]
    distances[0, 0] = value
    neighbors = [(1, 0), (-1, 0), (0, 1), (0, -1)]
    while heap and distances[m - 1, n - 1] == -1:
        dist, x, y = heappop(heap)
        for neighbor in neighbors:
            i, j = x + neighbor[0], y + neighbor[1]
            if i >= 0 and j >= 0 and i < m and j < n and maze[i, j] != -1 and distances[i, j] == -1:
                heappush(heap, (maze[i, j] + dist, i, j))
                distances[i, j] = maze[i, j] + dist
    print distances
    
    if distances[m - 1][n - 1] == -1:
        return
 
    x, y = m - 1, n - 1
    path = []
    while x != 0 or y != 0:
        path.append((x, y, distances[x, y]))
        x, y = getMinParent(distances, x, y, neighbors)
    path.append((0, 0, maze[0, 0]))
    
    return path

def getMinParent(distances, x, y, neighbors):
    minDist = distances[x, y]
    fromx, fromy = None, None
    for neighbor in neighbors:
        i, j = x + neighbor[0], y + neighbor[1]
        if i >= 0 and j >= 0 and i < len(distances) and j < len(distances[0]) and distances[i, j] >= 0 and distances[i, j] <= minDist:
            fromx, fromy = i, j
            minDist = distances[i, j]
    return fromx, fromy

A tester

minCostPath(np.array([[4,2,-1,1,1],
                      [1,-1,6,7,1],
                      [4,-1,3,0,1],
                      [4,7,3,10,1],
                      [4,8,6,-1,1],
                                ]))

- aonecoding May 06, 2018 | 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