Path Existence in an Undirected Graph
Updated
Problem statement
You are given an undirected graph with n vertices numbered from 0 to n - 1. The array edges contains one unordered pair [u, v] for each edge between vertices u and v.
Return true if a path connects source to destination. Otherwise, return false.
A vertex always has a path to itself, including an isolated vertex.
Function
validPath(n: int, edges: int[][], source: int, destination: int) → booleanExamples
Example 1
n = 3edges = [[0,1],[1,2],[2,0]]source = 0destination = 2return = trueThe edge between 0 and 2 directly connects the two vertices.
Example 2
n = 6edges = [[0,1],[0,2],[3,5],[5,4],[4,3]]source = 0destination = 5return = falseVertices 0 and 5 belong to different connected components.
Example 3
n = 1edges = []source = 0destination = 0return = trueThe source and destination are the same isolated vertex.
Constraints
1 <= n <= 200000.0 <= edges.length <= 200000.- Every edge contains two valid vertex numbers.
0 <= source, destination < n.- The graph may contain cycles, disconnected components, and repeated edges.
More Bloomberg LP Onsite Interview questions
- N-ary Index-Path Encoder and DecoderPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Top K Frequent WordsONSITE INTERVIEW · Seen Nov 2026
- Trapping Rain WaterPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Word SearchOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Design Browser HistoryPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Mutable Stock Price Top KPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Add Two Numbers IIPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Decode StringPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Minimum Replacements to Make Two Strings AnagramsPHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
- Currency Conversion RatePHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
Similar questions at other companies
- Count Friend CirclesOpenAI · PHONE SCREEN · ONSITE INTERVIEWOpenAI interview questions
- Count Connected Point ClustersGoogle · PHONE SCREEN · ONSITE INTERVIEWGoogle interview questions
- Count Connected Components in an Undirected GraphNutanix · ONSITE INTERVIEWNutanix interview questions
- Path Existence in an Undirected GraphByteDance · PHONE SCREENByteDance interview questions
- Connected GroupsVisa · OAVisa interview questions
- Evaluate DivisionApple · PHONE SCREENApple interview questions
- Count the Number of Complete ComponentsAmazon · PHONE SCREEN
- Visible Profiles CountMicrosoft · OAMicrosoft interview questions
- Number of ProvincesSambaNova Systems · PHONE SCREENSambaNova Systems interview questions
- Number of IslandsIntuit · ONSITE INTERVIEWIntuit interview questions
Bloomberg LP interview experiences
- Bloomberg AI-Assisted Coding Round, All Levels: 14 Experiences
- Bloomberg SWE Intern Interview Questions: 68 Questions
- Bloomberg Interview Questions, Level Not Reported: 65 Questions
- Bloomberg Experienced Design & More: 126 Questions (Part 2 of 2)
- Bloomberg Experienced SWE Interviews: 167 Questions (Part 1 of 2)
- Bloomberg New Grad Coding Questions: 121 Questions (Part 2 of 2)