Amazon | SDE-2 | Weird Experience

Difficulty: MEDIUMCategory: InterviewExperienceFrequency: 1Asked On 2025-01-25Added By Anonymous
Source: Link
Amazon | SDE-2 | Weird Experience

Recently I gave a screening interview for a SDE-2 position at Amazon. Interviewer was a hiring champion at Amazon according to their LinkedIn profile. I always check interviewer profile before giving the interview.

Experience:

Interview started with exchanging our intros First 20-25 mins went for Leadership Principle Questions 2 LP questions were asked and I answered them based on my experience by blending LPs into my answer. This LP session went well.

Then I was asked a simple graph question. Question: Given connections between different cities & some of them are data centers. Return the city with farthest network connection to any data center possible. Example that was given connections: [(a,b);(b,c);(c,d);(d,e);(f,g);(g,e)] dataCenters: [a,f]

He explained me the example as below

a - b - c - d - e - g - f (Cities) 0 - 1 - 2 - 3 - 2 - 1 - 0 (Distance from nearest data center)

As 'd' is farthest, expected output is 'd'

In my mind, I was happy thinking its a simple Multi-source BFS question that I can solve. I just explained my approach (BFS approach) and I said "Finally I will list all the cities that are farthest"

With a surprised face, interviewer asked me "Why list of cities, when there is only one farthest city ?" I told him "There can be multiple cities which are farthest, for example" and I modified his example as below

Modified example: connection: [(a,b);(b,c);(c,d);(d,e);(f,g);(g,e);(f,h)] dataCenters: [a,h]

a - b - c - d - e - g - f - h (Cities) 0 - 1 - 2 - 3 - 3 - 2 - 1 - 0 (Distance from nearest data center)

Now expected output is [d,e], since both are farthest

After explaining this example, interviewer said "Ok, there can be multiple cities"

And I started coding, explaining my process and code as I went ahead. Below was my exact code which I gave I also asked him if the entire graph is strongly connected or not, interviewer said its a SC graph

vector<char> getFarthestCities(vector<vector<char>> connections, vector<char> dataCenters) {
    unordered_map<char,vector<char>> edges;

    for (auto connection : connections) {
        edges[connection[0]].push_back(connection[1]);
        edges[connection[1]].push_back(connection[0]);
    }

    vector<int> dist(edges.size(),INT_MAX);
    queue<pair<int,int>> q;

    for (auto dc : dataCenters) {
        q.push({dc-'a',0});
    }

	// Loop-1
    while (!q.empty()) {
        auto current = q.front();
        q.pop();

        char city = current.first+'a';
        int distance = current.second;

		// Loop-2
        for (auto neighbour : edges[city]) {
            if (dist[neighbour-'a']>1+distance) {
                dist[neighbour-'a']=1+distance;
                q.push({neighbour-'a', 1+distance});
            }
        }
    }

    int maxDist=*max_element(dist.begin(),dist.end());
    vector<char> farthestCities;
    int s=dist.size();

	// Loop-3
    for (int i=0;i<s;i++) {
        if (dist[i]==maxDist) {
            farthestCities.push_back(i+'a');
        }
    }

    return farthestCities;
}


I dry ran the example and explained every step, interviewer said "Ok good" One thing I noticied is that. Interviewer looked confused through out the interview. In my mind, I was thinking "You are a hiring champion, how can you be this confused for a simple BFS question".

Now very weird debate happened. Interviewer asked me to place Loop-3 in Loop-2. I told the intervier its a bad step, because there will be loop within a loop, which can be similar to O(N^2) Interviewer said "No, it won't because N in inner loop will be small"

My lower jaw literally hit the floor after listening this. I had a debate, I told the interviewer we should never think this way by just seeing a single example, there can be cases which can make it worse. Interviewer didn't agree with me. I explained an edge case where this won't be the case. Interviewer kind of agreed after giving a edge case.

During this discussion only one thing was going in my head ("How did you become a hiring champion").

Finally, instead of having a loop within a loop. I eliminated Loop-3 by making some temporary variables to store the max distance and list of cities. Finally, the interviewer agreed on the solution.

Feedback that I received: Positive for Leadership Principles Negative for Technical Skills, mentioning there are gaps in technical skills. Rejected

Am I wrong any where ?

Comments