Google Interview Question for Java Developers


Country: United States




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

Ans is not 16. I think it is 11 for this question.

- shrishty February 24, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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.

- denis.zayats February 24, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

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;
    }

- denis.zayats February 19, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

looks like a dynamic programming question

- shrishty February 24, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

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.

- shrishty February 24, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

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;
}

- shrishty February 24, 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