This question evaluates understanding of graph algorithms and pathfinding, covering tree diameter computation and grid shortest-path problems as well as enumeration of simple paths, and requires skills in traversal techniques, combinatorial reasoning, and time/space complexity analysis.

You are interviewing for a software engineer / ML intern role and are given the following algorithmic problems.
You are given a tree, i.e., a connected, undirected, acyclic graph with nodes labeled from 1 to , and edges.
Each edge is unweighted (or equivalently has weight 1).
Task:
Find the length of the longest path between any two nodes in the tree. The length of a path is defined as the number of edges on that path.
You should describe an algorithm (no need to provide code) and analyze its time and space complexity.
Assume , so the algorithm must be close to linear time.
You are given an grid representing a 2D map. Each cell is either free or blocked:
0
means the cell is free.
1
means the cell is blocked (an obstacle) and cannot be stepped on.
You start in the top-left cell (0, 0) and want to reach the bottom-right cell (n - 1, n - 1).
From any cell, you may move one step at a time in the four cardinal directions:
(r - 1, c)
(r + 1, c)
(r, c - 1)
(r, c + 1)
You may only move to cells that are within bounds and free (0).
Task:
Find the length (in number of steps) of the shortest path from (0, 0) to (n - 1, n - 1).
-1
.
grid
of 0s and 1s.
(n - 1, n - 1)
from
(0, 0)
, or
-1
if impossible.
Describe the algorithm you would use and analyze its time and space complexity.
Now, suppose we want to find all possible paths from (0, 0) to (n - 1, n - 1) under the same movement and obstacle rules.
You may assume is small enough that, in principle, all paths could be generated for the sizes considered (e.g., in a whiteboard or interview setting), but you should still comment on scalability and limitations.