You are given an interface representing a node in an unweighted graph:
interface Candidate {
String id();
List<Candidate> getConnections();
}
Implement:
int getDistance(Candidate c1, Candidate c2)
where getDistance returns the length of the shortest path (minimum number of edges) between c1 and c2 using the getConnections() adjacency list.
0
if
c1
and
c2
are the same node.
-1
if
c2
is unreachable from
c1
.
If this needs to run in production at large scale (very large graph, frequent queries), discuss how you would optimize and/or redesign the solution (data structures, precomputation, caching, storage, distributed concerns).
You are given a JSON-like config mapping service name → I/O paths:
{
"svcA": {"read_from": "/path123", "write_to": "/path456"},
"svcB": {"read_from": "/path456", "write_to": "/path789"}
}
You are also given a list of deleted paths:
deleted_list = ["/path123", "/path12", ...]
Return all services that are impacted by the deletions.
A service is directly impacted if either its read_from or write_to is affected by deleted_list.
In addition to exact matches, apply this rule:
read_from
or
write_to
path shares a
common parent directory
with a deleted path, treat it as impacted.
(You should clearly define what you consider the “affected subtree root” and implement consistently, e.g., based on nearest common parent directories / minimal affected roots.)
Services form a dependency chain via produced/consumed paths:
X.write_to == Y.read_from
, then
Y
depends on
X
(data flows from
X
→
Y
).
For each entry in deleted_list, output the impacted services in dependency order (from upstream producers to downstream consumers). If multiple services are impacted independently, output them in a valid topological order.
read_from
and
write_to
are single paths per service.
/
separators.