Google Interview Question for Front-end Software Engineers






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

It is a two dimension dp problem.

- navin August 16, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

sorry, the robot has to start in upper left and get to the bottom right square. Interviewer wanted an algorithm to compute the number of paths, rather than just coming up with a mathematical answer. She also seemed pleased that my solution provided not only the number of paths, but the paths themselves.

- bjh August 16, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

What was your solution?

- Harit August 16, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I dont have time to rewrite the code, but I essentially wrote a recursive function like this:

function findPath(curPath) { ... }

It then looks down and right, duplicates the curPath to pathRight and pathDown, pushes the next cell onto those paths and then calls itself for both right and down (assuming there is no wall). When the exit tile is found, I push the curPath onto a global array of paths. When done, that global arry contains all the paths

- bjh August 18, 2010 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

A robot can take path (N,M) from either (N-1,M) or (N,M-1)
if N and M is 0, its the beginning so return 0.
if N or M is 0, we can get there in 1 path.

We call paths(N,M) with s[][] initialized to -1.

int paths(int i,int j)
{
if(!(i||j))
return 0;

if(!(i&&j))
return 1;

if(s[i][j] != -1)
return s[i][j];

s[i][j] = paths(i-1,j) + paths(i,j-1);

return s[i][j];
}

- V August 18, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

# assume maze_env is an object that knows
# all about the geometry of the maze,
# end point is (0,0)

# maze_env defined somewhere above
paths_cache = {}
def get_maze_paths(x, y):
  # retrieve memoized solution
  if (x,y) in paths_cache:
    return paths_cache((x,y))
  if x, y == 0, 0:
    return []
  if x and maze_env.can_go_right(x, y):
    right_maze_paths = get_maze_paths(x-1, y)
  if y and maze_env.can_go_down(x, y):
    down_maze_paths = get_maze_paths(x, y-1)
  maze_paths = [['R'] + path for path in right_maze_paths + ...
                ['D'] + path for path in down_maze_paths]
  # memoize solution for (x,y)
  paths_cache[(x,y)] = maze_paths
  return maze_paths

- Bullocks September 06, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Is there an API that tells me where are the walls?
Do you want to go from top-left to bottom right?
...

- S August 15, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int paths(int m, int n)
{
    int sum=0;
    int i;

    if(!(m || n)) 
    {   
        return 0;
    }   

    if(m == 1)
    {   
        return n + 1;
    }   

    if(n == 1)
    {   
        return m + 1;
    }   

    for(i = 0; i <= n; i++)
        sum += paths(m - 1, n - i); 

    return sum;
}

- Siddique August 17, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

int paths(int m, int n)
{
    int sum=0;
    int i;

    if(!(m || n)) 
    {   
        return 0;
    }   

    if(m == 0)
    {   
        return 1;
    }   

    if(m == 1)
    {   
        return n + 1;
    }   

    for(i = 0; i <= n; i++)
        sum += paths(m - 1, n - i); 

    return sum;
}

- Siddique August 17, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

first fill first row and first column as 0 1 1 1.. as for 1st row only way to reach a column is by moving to right -> so and for column moving down from previous column

now for(i=1;i<n;i++)
for(j=1;j<m;j++)
a[i][j]=a[i-1][j]+a[i][j-1];

a[n][m] gives the ways to reach that box in matrix.

- gaurav August 21, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

isn't it
(m+n) choose n?

- q August 22, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

It is only if there are no walls.

- Anonymous September 12, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

As one said above, this is a 2 dimensional dp question (Actually very typical).
So if you use recursive, that's probably not a good answer...Scan the matrix row by row will be better, and will also reduce the use of space since you only need two rows, rather than the whole matrix to save the temporary result.

- jimmyzzxhlh August 25, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

And I thought recursion was the crux of DP, no?

- Anonymous August 30, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

its foolish to find all the paths. Since the number of paths would be exponential if the maze was randomly generated. How come the interviewer was impressed when you decided to store all paths.Wish the interviewers are not so dumb. Rather than that it would have been better if you were asked to print only 1 path.

- Anonymous October 12, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

its foolish to find all the paths. Since the number of paths would be exponential if the maze was randomly generated. How come the interviewer was impressed when you decided to store all paths.Wish the interviewers are not so dumb. Rather than that it would have been better if you were asked to print only 1 path.

- Anonymous October 12, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <iostream>
void PrintRobotPaths(std::string path, int row, int column, int totrows, int totcols)
{
	if(column == totcols && row == totrows)
	{
		std::cout << path << std::endl;
		return;
	}
	if(row == totrows)
	{
		std::string pc = path + " D";
		PrintRobotPaths(pc, row, column+1, totrows, totcols);
		return;
	}
	if(column == totcols)
	{
		std::string pr = path + " R";
		PrintRobotPaths(pr, row+1, column, totrows, totcols);
		return;
	}
	std::string pr = path + " R";
	std::string pc = path + " D";
	PrintRobotPaths(pr, row+1, column, totrows, totcols);
	PrintRobotPaths(pc, row, column+1, totrows, totcols);
}

int main(int argc, char** argv)
{
	int row=4;
	int column=4;
	std::string path;
	PrintRobotPaths(path,1, 1, row, column);
	return 0;
}

- limaye.nikhil November 15, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

No of paths: (n+m)! / n!.m!

- P December 04, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Here is one way to do it.

public static void displayAllPaths(final int rows, final int columns, int curRow, int curCol, char[] path) {
        if (curRow == rows - 1 && curCol == columns - 1) {
            for (int i = 0; i < path.length; i++) {
                System.out.printf("%3c", path[i]);
            }
            System.out.println();
            return;
        }

        if (curCol < columns - 1) {
            path[curRow + curCol] = 'R';
            displayAllPaths(rows, columns, curRow, curCol + 1, path);
        }

        if (curRow < rows - 1) {
            path[curRow + curCol] = 'D';
            displayAllPaths(rows, columns, curRow + 1, curCol, path);
        }
    }

- Singleton June 01, 2012 | 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