Tech & SaaS
Senior
programming
LeetCode #1971 - Find if Path Exists in Graph
Given an undirected graph represented as an array of edges, write a function to check if there is a valid path between two nodes (the start and the end nodes).
Input:edges = [[0,1],[1,2],[2,3],[3,4]]
startNode = 0
endNode = 4
Output:true
Constraints:1 <= edges.length <= 10^4
0 <= edges[i][0], edges[i][1] < 10^4
```
## Approach
Use Depth First Search (DFS) or Breadth First Search (BFS) to traverse the graph, maintaining a visited set to avoid cycles. If you reach the end node during traversal, return true. Otherwise, return false after traversal completes.
## Complexity
**Time:** O(E) where E is the number of edges, as each edge is checked once.
**Space:** O(V) where V is the number of vertices for the visited set.
## Trade-offs
- Using DFS has a stack-space overhead, while BFS uses explicit queue space.
```
Trusted by 100+ professionals preparing for interviews
Trusted by 100+ professionals
50+ Company Question Banks
5+ Supported Languages
Practice More Questions Like This
Generate unlimited interview questions with structured answers, code runner, and AI-powered walkthroughs.
Get Started Free
More Tech & SaaS Interview Prep
LeetCode #123 - Best Time to Buy and Sell Stock III
Tech & SaaS · Senior
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.