MEDIUMasked at 1 company

Find Closest Node to Given Two Nodes

A medium-tier problem at 53% community acceptance, tagged with Depth-First Search, Graph. Reported in interviews at Juspay and 0 others.

Founder's read

You're given a directed graph and two nodes. Find the node closest to both of them. It's a medium-difficulty problem that sits at the intersection of graph traversal and distance calculation. Juspay has asked it. The acceptance rate hovers around 53 percent, which means half the candidates either nail the pattern or stumble on how to efficiently compute distances from two sources. This is the kind of problem that looks simple until you realize you need to think about what 'closest' actually means in a graph context. If this problem hits your live assessment and you blank on the dual-source traversal logic, StealthCoder surfaces a working solution in seconds, invisible to the proctor.

Companies asking
1
Difficulty
MEDIUM
Acceptance
53%

Companies that ask "Find Closest Node to Given Two Nodes"

If this hits your live OA

Find Closest Node to Given Two Nodes is the kind of problem that decides whether you pass. StealthCoder reads the problem on screen and surfaces a working solution in under 2 seconds. Invisible to screen share. The proctor sees nothing. Built by a senior engineer who knows the OA is theater. This is the script.

Get StealthCoder
What this means

The trick is running depth-first search (or breadth-first search) from both source nodes separately, then finding the node that minimizes the maximum distance to either source. Most candidates jump to a single-source approach and waste time reworking it. The real gotcha: a node might be reachable from one source but not the other, so you need to handle unreachable cases. You're doing two separate graph traversals and then comparing results across all nodes. The DFS approach is straightforward, but getting the distance calculation and comparison logic tight is where errors creep in. When you hit this on a live OA and haven't drilled the two-source pattern, StealthCoder runs invisibly and hands you the correct traversal and comparison logic so you can code it out and submit.

Pattern tags

The honest play

You know the problem. Make sure you actually pass it.

Find Closest Node to Given Two Nodes recycles across companies for a reason. It's medium-tier, and most candidates blank under the timer. StealthCoder is the hedge: an AI overlay invisible during screen share. It reads the problem and surfaces a working solution in under 2 seconds. Built by a senior engineer who knows the OA is theater. This is the script. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Find Closest Node to Given Two Nodes interview FAQ

What does 'closest' mean if I have two starting nodes?+

You're looking for the node that minimizes the maximum distance to either source node. So if node X is distance 3 from source A and distance 5 from source B, its score is 5. You want the node with the smallest such score. Ties are broken by node value.

Do I need to use BFS or is DFS fine?+

DFS works because you're computing distances in a directed graph, not a weighted one. BFS is slightly cleaner for distance tracking, but both will get you there. Pick whichever feels natural. The real work is running the traversal twice.

What if a node isn't reachable from one of the sources?+

That node can't be the answer. You need to filter out unreachable nodes before comparing. Only consider nodes reachable from both sources, or handle the unreachable case explicitly in your distance comparison.

Is this problem still asked at companies?+

Juspay has reported asking it. At 53 percent acceptance, it's neither trivial nor a rare curveball. It's a solid medium-level graph problem that tests whether you can structure a multi-source search cleanly.

How does this relate to the graph topics I should know?+

It's a direct application of depth-first search on a graph. You need to be comfortable traversing directed graphs, tracking visited nodes, and computing path distances. If you can do DFS, this is pattern recognition more than raw difficulty.

Want the actual problem statement? View "Find Closest Node to Given Two Nodes" on LeetCode →

Frequency and company-tag data sourced from public community-maintained interview-report repos. Problem, description, and trademark © LeetCode. StealthCoder is not affiliated with LeetCode.