Google Interview Question
Java DevelopersCountry: United States
Explain your answer in the code, please.
My explanation is:
1. Assume that we know that we need to start on the first line from left to right because we know that the last broken computer on the paired line (if to calculate from 1, not from 0) and it is closer to the right side.
2. It means that will be faster if we start the verification of the last line from the right side.
3. We have 5 rows of the 5 items in a row. The last broken computer is a first item in the 4th row. It means that 3*5 + 1 = 16.
ArrayList<int[]> coordinatesBrokenComputers = new ArrayList<>();
coordinatesBrokenComputers.add(new int[]{1,1});
coordinatesBrokenComputers.add(new int[]{1,4});
coordinatesBrokenComputers.add(new int[]{2,2});
coordinatesBrokenComputers.add(new int[]{3,4});
int[][] room = new int[][]{
{1,1,1,1,1},
{1,0,1,1,0},
{1,1,0,1,1},
{1,1,1,1,0},
{1,1,1,1,1}
};
assertEquals(16, coordinatesFounder.getLessAmountOfStepsToFindBrokenComputers(room, coordinatesBrokenComputers));
Implementation:
public int getLessAmountOfStepsToFindBrokenComputers(int[][] room, ArrayList<int[]> coordinatesBrokenComputers) {
int steps = 0;
int sizeOfBrokenComputers = coordinatesBrokenComputers.size();
for (int i = 0; i < room.length; i++) {
if (i % 2 == 0) {
for (int j = 0; j < room[i].length; j++) {
steps++;
if (room[i][j] == 0) {
sizeOfBrokenComputers--;
if (sizeOfBrokenComputers == 0) {
return steps;
}
}
}
} else {
for (int j = room[i].length - 1; j >= 0; j--) {
steps++;
if (room[i][j] == 0) {
sizeOfBrokenComputers--;
if (sizeOfBrokenComputers == 0) {
return steps;
}
}
}
}
}
return steps;
}
The code can be beautified. I have assumed matrix size is 5x5 but it can be easily generalized.
int[][] room = new int[][]{
{1,1,1,1,1},
{1,0,1,1,0},
{1,1,0,1,1},
{1,1,1,1,0},
{1,1,1,1,1}
};
this is the sequence of steps - (1, 0), (1, 1), (1, 2), (1,3),(1,4), (2,4), (2,3),(2,2),(2,3),(2,4),(3,4)
All computers are repaired at this point.
This is the recursive solution. Can easily be mapped to dynamic programming
#include <iostream>
#include <vector>
#include <unordered_map>
using namespace std;
int minSteps(int M[5][5], int index, unordered_map<int, int> m, bool left)
{
cout<<"index = "<<index<<" isleft = "<<left<<endl;
if(index >= 5)
{
cout<<"Recursion ended"<<endl;
return 0;
}
int steps = 0;
if(m.find(index) != m.end())
{
if(left)
{
for(int j = 0; j < 5; j++)
{
steps++;
if(M[index][j] == 0)
{
m[index] -= 1;
if(m[index] == 0)
{
return min(5 + minSteps(M, index+1, m, false),
2 * steps - 1 + minSteps(M, index + 1, m, true));
}
}
}
}
else
{
for(int j = 4; j >=0; j--)
{
steps++;
if(M[index][j] == 0)
{
m[index] -= 1;
if(m[index] == 0)
{
return min(5 + minSteps(M, index + 1, m, true),
2 * steps - 1 + minSteps(M, index + 1, m, false));
}
}
}
}
}
else
{
if(left)
{
return min(5 + minSteps(M, index + 1, m, false),
0 + minSteps(M, index + 1, m, true));
}
else
{
return min(5 + minSteps(M, index + 1, m, true),
0 + minSteps(M, index + 1, m, false));
}
}
return 0;
}
int main()
{
vector<pair<int, int>> ls = { {1, 1}, {1, 4}, {2, 2}, {3, 4}};
int R, C;
R = 5; C = 5;
int M[5][5];
unordered_map<int,int> m;
for(int i = 0; i < R; i++)
{
for(int j = 0; j < C; j++)
{
M[i][j] = 1;
}
}
for(auto itr: ls)
{
M[itr.first][itr.second] = 0;
if(m.find(itr.first) == m.end())
{
m[itr.first] = 1;
}
else
{
m[itr.first] += 1;
}
}
cout<<minSteps(M, 0, m, true)<<endl;
return 0;
}
Ans is not 16. I think it is 11 for this question.
- shrishty February 24, 2018