Amazon Interview Question for SDE1s


Country: India
Interview Type: In-Person




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

You can see the matrix as a graph, with the doors being edges. Then simply do a BFS from the room you're at, and once you hit a bathroom, return its location.

- Anonymous July 11, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 2 votes

+1 on the BFS approach.

- marvince.reyes July 12, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Here's an implementation of @Anonymous suggestion in C++

#include<stdio.h>
#include<iostream>
#include<string.h>

#define N 4
#define M 5

using namespace std;

// Prints the nearest bathroom

void printNearestBathroom(int rowPos,int colPos)    {
    cout<<"The nearest bathroom is at:"<<rowPos<<" "<<colPos;
    return;
}


void bfs(int matrix[][M],int startPosRow,int startPosCol)   {
    
    int queue[N*M][2];
    
    bool visited[N][M];
    
    for(int i=0;i<N;i++) {
        for(int j=0;j<M;j++)    {
            visited[i][j]=false;
        }
    }
    
    int front,rear;
    front=rear=-1;
    queue[++rear][0]=startPosRow;
    queue[rear][1]=startPosCol;
    visited[startPosRow][startPosCol]=true;
    front++;
    
    while(front<=rear)  {
        
        int currRow=queue[front][0];
        int currCol=queue[front][1];
        
        if(matrix[currRow][currCol]==0) {
            printNearestBathroom(currRow,currCol);
            return;
        }
        
        front++;
        
        
        if( currRow>0 && visited[currRow-1][currCol]==false )  {
            queue[++rear][0]=currRow-1;
            queue[rear][1]=currCol;
            visited[currRow-1][currCol]=true;
        }
        
        if(currCol>0 && visited[currRow][currCol-1]==false ) {
            queue[++rear][0]=currRow;
            queue[rear][1]=currCol-1;
            visited[currRow][currCol-1]=true;
        }
        
        if(currCol!=M-1 && visited[currRow][currCol+1]==false )   {
            queue[++rear][0]=currRow;
            queue[rear][1]=currCol+1;
            visited[currRow][currCol+1]=true;
        }

        if(currRow!=N-1 && visited[currRow+1][currCol]==false )    {
            queue[++rear][0]=currRow+1;
            queue[rear][1]=currCol;
            visited[currRow+1][currCol]=true;
        }
        
    }
    
}
int main()  {
    int matrix[N][M]={ {1,1,1,1,0},        // 1 denotes a room and 0 denotes a bathroom
                       {1,1,1,1,1},
                       {1,1,0,1,0},
                       {1,1,0,1,0}
                       };
    bfs(matrix,2,1);
    return 0;
}

- Sibendu Dey July 18, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

One way is to maintain a count variable and recursively call for the left, right, bottom and up from the room and increment the count unless a bathroom is reached. Once reached compare the count with max. If less than max then update. Finally the minimum is the answer.

Second is using DP, which needs to be modified to start from the given room.

- Nascent July 11, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

BFS approach as the question is to find the nearest ....

- sarath s pillai July 12, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Since there are multiple bathrooms available, I think it is better if we consider a single-source shortest path algorithm other than BSF/DFS. As a result, we will find the distance to all the bathrooms. The answer to this problem is then the shortest path found in the process. If we are ensured that weights are all positive, then Dijkstra can be used safely. Otherwise, it is a safer bet to use Bellman-Ford algorithm to address the possibility of existence of any negatively weighted edge.

- Anonymous August 20, 2013 | 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