Download Cracking the Coding Interview, 6th Edition PDF Computer and Tchnollogy Books Elkins. Work at Google — Example Coding/Engineering Interview - Duration: 24:02. Life at Google 4,255,542. JOxca.FREE READ DOWNLOAD Cracking the Coding Interview, 6th Edition: 189 Programming Questions and Solutions by Gayle Laakmann McDowell K.I.N.D.L.E.
Problem States: 'Imagine a robot sitting on the upper left corner of grid with r rows and c columns. Robot can only move in 2 directions, right and down, but certain cells are 'off limits' such that the robot cannot step on them. Design an algorithm to find a path for the robot from the top left to the bottom right.'
My solution was to do something like the following:
----after both while loops I'll then throw in a recursive call to the function with the new r,c coordinates.
It seems like a pretty simple implementation, but the answer key has a pretty lengthy and complicated solution. Can anyone explain to me why mine doesn't work?
It wouldn't work if you had a 'map' like this (where |
is the path and *
is where the robot would end up)
The 'robot' would move as far down as possible and then would be unable to move right and your algorithm would fail.
I would recommend reading up on tree traversal, especially A*.
Easy. About 10 lines of code, and time proportional to rows x columns.
Definition: A cell is 'fine' if you can reach the bottom right corner from it. In a first pass, you determine all the cells that are fine. Just examine the rows from top to bottom, the columns of each row from right to left: A cell is fine if it is not 'off limits', and if it either is the bottom right corner, or if it is above or to the left of a cell that is fine.
If the top left corner is not fine then there is a solution. Otherwise, start with the top left corner. And as long as you haven't reached the bottom right corner, at every turn you go either down or right to a cell that is fine.
try using bfs or dfs. Consider this problem as: shortest path from one point to another.Using recursion and visited array, that is dfs can resolve this problem quickly.