Amazon Interview Question for SDE1s


Country: India
Interview Type: In-Person




Comment hidden because of low score. Click to expand.
2
of 4 vote

1) Build a graph of (NxN) vertices that there exist an edge (v->w) if the knight can move from v to w. The maximum order of a vertices is 8 so the maximum number of edge is 8xNxN.
We perform the BFS from the source to find
- If the destination is reachable from the source
- if there're a path, then BFS give the minimum number of hops

Node : there're no need to build the graph explicitly we only want to do the BFS on the implicit graph.

2. I believe for infinite chess board all the positions are reachable so
- the solution is not extendable if the destination is unreachable from the source
- Otherwise, the solution on the infinite chess board is at least as good as NxN that gives us the upper bound for the search on the infinite chess board. Again we can rerun the BFS with the bound from the previous step to check if we can find a better solution.

- LinhHA05 October 08, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Nice solution, complexity O(V+E).
V = N
E = 8N

far better than recursive algo which takes exponential time.

- varun October 08, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

What's the pseudocode for recursive exponential algorithm?

- S O U N D W A V E October 08, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

BFS seems overkill

- Anonymous October 14, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Anonymous: what is your propose solution to find the minimum number of hops, I think BFS is the simplest one to do that

- LinhHA05 October 14, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

I think you should use BFS rather DFS as BFS will find min no.of hops

- snigda October 08, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yes you are correct. It was a typo mistake.

- Vin October 08, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 2 vote

A* for question 2

- zhangzizhong0828 October 08, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

For 2), I want to place the piece at (inf, inf)
and I want to know the hops for it to reach (-inf, inf).

- S O U N D W A V E October 08, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 2 vote

This pseudocode is for the case of a board of -infinity to + infinity
it also works for n>2 except for the corner cases (which need additional conditions)
Requirement - board origin = 0,0

int hops;
int quotient = (abs(p-x) + abs(q-y))/3;
int remainder = (abs(p-x) + abs(q-y))%3;

int adjustment = 0
if (remainder == 1) then adjustment = 3;
else if (remainder ==2) then adjustment = 2;

hops = quotient + adjustment;

-- validated the above with http :// tacticstime . com/wp-content/uploads/2011/09/chess_knight_all.png
-- There seems to be some cases when x=p or y=q that this does not give the least hops (eg. (x,y)=(0,0) and (p,q) = (4,0), so some additional conditions needed there as well.

- IC October 08, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Isnt it (N-x)? Knight is already at position x. (N-x)=p

- Anonymous October 09, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

You assumed (p,q) is reachable from (x,y). What if (p,q) not reachable?

- Anonymous October 09, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

that is just consider the special case whether x,y is out side of nxn, minor change

- odie October 14, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

The A* Algorithm would work here, I think. For each of the squares you can reach from the start position, get the linear distance to the goal and add 1. Select the square where this value is the smallest and repeat, adding 2 instead of 1. The function for A* is g(x)+h(x), where g(x) is a cost function (here, merely a number of moves) and h(x) is a heuristic function (we decided on linear distance to the goal) for a square x.

- Anonymous October 09, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

The beauty of the algorithm is that it is guaranteed to reach the goal state with the least amount of exploration. Didn't mention that before, sorry.

- Anonymous October 09, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Why not use BFS even on an infinite board? You don't need to construct the graph, you just know what are the neighbors for each field. And even with A* you have growing data structures to maintain.

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

I think its Recursive DP problem.

- KnowledgeSeeker October 13, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

do you have a C solution?

- Anonymous October 23, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

rrr

- rrr October 08, 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