Optimize Leaper Graph Algorithm?

During a 45-minute technical interview with Google, I was asked about the Leaper Graph issue. I wrote working code, but a job offer was later rejected because I lacked data on the data structure. I wonder what I could do better.

The problem was this: β€œGiven the size board N, and said that I can jump horizontally (left or right) and j vertically (up or down) (ie, like a horse in chess), the leader can reach every place on the board? "

I wrote the following algorithm. It recursively discovers that each position on the board is reachable, marking all the points on the chart that have been visited. If this failed, then at least one field was false and the function will return false.

static boolean reachable(int i, int j, int n) { boolean grid[][] = new boolean[n][n]; reachableHelper(0, 0, grid, i, j, n - 1); for (int x = 0; x < n; x++) { for (int y = 0; y < n; y++) { if (!grid[x][y]) { return false; } } } return true; } static void reachableHelper(int x, int y, boolean[][] grid, int i, int j, int max) { if (x > max || y > max || x < 0 || y < 0 || grid[x][y]) { return; } grid[x][y] = true; int i2 = i; int j2 = j; for (int a = 0; a < 2; a++) { for (int b = 0; b < 2; b++) { reachableHelper(x + i2, y + j2, grid, i, j, max); reachableHelper(x + j2, y + i2, grid, i, j, max); i2 = -i2; } j2 = -j2; } } 

Now, it was later indicated that the best solution would be to implement a joint implementation of Donald Knuth: http://arxiv.org/pdf/math/9411240v1.pdf Is this what you need to be able to find out in a 45-minute technical interview?

Other than the above, is there anything I could do better?

change
- I asked about the starting position. I was told that starting with 0.0 is good.

edit2 Based on the feedback, I wrote a while loop with the approach to the queue. A recursive approach is performed when the stack overflows at n = 85. However, the while loop with the queue method is lower to ~ n = 30,000. (After that, it encounters heap problems with more than GB memory). If you know how to optimize further, please let me know.

  static boolean isReachableLoop(int i, int j, int n) { boolean [][] grid = new boolean [n][n]; LinkedList<Point> queue = new LinkedList<Point>(); queue.add(new Point(0,0)); // starting position. int nodesVisited = 0; while (queue.size() != 0) { Point pos = queue.removeFirst(); if (pos.x >= 0 && pos.y >= 0 && pos.x < n && pos.y < n) { if (!grid[pos.x][pos.y]) { grid[pos.x][pos.y] = true; nodesVisited++; int i2 = i; int j2 = j; for (int a = 0; a < 2; a++) { for (int b = 0; b < 2; b++) { queue.add(new Point(pos.x+i2, pos.y+j2)); queue.add(new Point(pos.x+j2, pos.y+i2)); i2 = -i2; } j2 = -j2; } } } } if (nodesVisited == (n * n)) { return true; } else { return false; } } 
+7
java algorithm recursion dynamic-programming knuth
source share
2 answers

I ask a lot of such questions for interviews. I don’t think that you expected that during the interview you will find out the mutual use method, but I would fix you for using the O (n ^ 2) stack space - especially since you passed all these parameters for each recursive call instead of using the object.

I would ask you this and expect you to come up with BFS or DFS using the stack or queue on the heap. If you fail in this, I may have a complaint, for example, "lack of knowledge about the data structure."

I would also ask questions to make sure you know what you are doing when you allocated this 2D array.

If you were really good, I would ask you if you can use the symmetry of the problem to reduce the search space. You really only need to look for a grid of size J * J (provided that J> = i).

It is important to remember that the interviewer is not just looking at your answer. He looks at how you solve problems and what tools you have in your brain that you can bring to solve.

Edit: Thinking about this a little more, there are many additional steps towards a collaborative method that you can come up with as well. No one will expect this, but it will be impressive!

+3
source share

Sorry, it seems to me that I missed something.

If you can only go up or down on i and left or right on j, then the case (x, y) is available from the starting example (a, b) if there are integers m and n, so

a + m * i = x

b + n * j = y

That is, everything is false for a square board, where n> 1.

If you mean more than a knight in chess, and you can go up / down on i and left / right on j OR up / down on j and left / right on i, you can use the same technique. It just turns into 2 equations:

a + m * i + n * j = x

b + o * i + p * j = y

If there are no integers m, n, o and p that satisfy these equations, you cannot reach this point.

+2
source share

All Articles