Google | Onsite round 1 | L3

Difficulty: MEDIUMCategory: InterviewExperienceFrequency: 1Asked On 2024-11-07Added By Anonymous
Source: Link

Company: Google
Role: L3
Interview Stage: Onsite
Location: unknown
Year: unknown

Original Post Content:

I had my 1st onsite round yesterday and this was the problem:

part 1)
You are given an infinite size chessboard (coordinates vary from -infinity to +infinity), and a knight initially at a given start point. You need to tell the minimum number of moves required for a knight to reach a given end point.
(NO further constraints were provided even after asking)

Solution: simple BFS, take map<point, moves> to keep track of points visited and the number of moves required to reach here, as you can't define any grid here.

part 2) You are also given a list of blocked points where the knight can't go.

Solution I told:
While considering the neighbor points in the above solution, just add one more condition: if that neighbor belongs to blocked points or not.

TWIST: This will not work. Think about a case where your endpoint is surrounded by blocked points from all sides, making it impossible for the knight to reach the end point.

Here, since the grid is infinite, you will always keep adding new neighbors into your queue, and you will never be able to come out of that loop. There will be no upper limit on grid size to stop it.

I was unable to think about this condition: to know when to stop searching further and consider that it's impossible to reach the end point.

Let me know if anyone knows about this.

Thanks in Advance!

Comments