Longest Distance in a Frequency-Limited Network
Updated
Problem statement
A network has networkNodes devices, numbered 0 to networkNodes - 1. Link i joins devices networkFrom[i] and networkTo[i] and works in both directions. Device i uses frequency frequency[i].
A link can carry a message only when the frequencies of its two devices differ by at most 1. Two devices can communicate when some path of these usable links joins them.
The distance between two devices that can communicate is the smallest number of usable links on a path between them.
Return the largest distance over all pairs of devices that can communicate. If no two devices can communicate, return 0.
Function
maxCommunicationDistance(networkNodes: int, networkFrom: int[], networkTo: int[], frequency: int[]) → intExamples
Example 1
networkNodes = 5networkFrom = [0,1,2,3]networkTo = [1,2,3,4]frequency = [1,2,3,2,1]return = 4The devices form a line 0 - 1 - 2 - 3 - 4 with frequencies 1, 2, 3, 2, 1. Each link joins frequencies that differ by 1, so every link can be used.
Devices 0 and 4 can talk, and the shortest usable path between them has 4 links. No pair is farther apart, so the answer is 4.
Example 2
networkNodes = 4networkFrom = [0,1,2,0]networkTo = [1,2,3,3]frequency = [1,3,2,1]return = 3The link 0 - 1 joins frequencies 1 and 3, which differ by 2, so it cannot be used. The links 1 - 2, 2 - 3 and 0 - 3 can be used.
Device 0 reaches device 1 only through 0 - 3 - 2 - 1, which has 3 links. That is the largest distance, so the answer is 3.
Example 3
networkNodes = 6networkFrom = [0,1,2,3,4]networkTo = [1,2,3,4,0]frequency = [2,2,2,2,2,3]return = 2Devices 0 to 4 form a ring and all have frequency 2. Device 5 has no links, so it cannot talk to anyone.
On a ring of 5 devices, the two ways around always give one path of at most 2 links. So the farthest pair is 2 links apart, and the answer is 2.
Example 4
networkNodes = 3networkFrom = [0,1]networkTo = [1,2]frequency = [1,3,1]return = 0Both links join frequency 1 and frequency 3, so neither can be used. No two devices can talk, so the answer is 0.
Constraints
1 <= networkNodes <= 10000 <= networkFrom.length == networkTo.length <= 30000 <= networkFrom[i], networkTo[i] < networkNodesandnetworkFrom[i] != networkTo[i]- No two links join the same pair of devices.
frequency.length == networkNodes- Each
frequency[i]is1,2or3.
More Bloomberg LP interview questions
- Word SearchOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Maximum Redundancy After One Segment FlipOA · Seen Sep 2026
- Sum of Pipeline Response TimesOA · Seen Sep 2026
- Merge IntervalsOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Aug 2026
- Design an Ordered StreamOA · PHONE SCREEN · ONSITE INTERVIEW · Seen May 2026
- Count Teams Outscoring a Target TeamOA · Seen Feb 2023
- Ashley Loves Numbers (Financial Engineer)OA · Seen Nov 2019
- Consecutive Sum (Financial Engineer Intern)OA · Seen Nov 2019
- Find Longest ChainOA · Seen Sep 2017
- N-ary Index-Path Encoder and DecoderPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- Delivery Management SystemAkuna Capital · OAAkuna Capital interview questions
- Shortest Path from an Encoded GraphIntuit · OAIntuit interview questions
- Find Minimum City HopsAmazon · OA
- Grid ExplorationIMC Trading · OAIMC Trading interview questions
- Shortest Route Through a Traced MazeDuolingo · OADuolingo interview questions
- Shortest Path in an Unweighted GraphPeopleStrong · OAPeopleStrong interview questions
- Drone Delivery with Battery Swap StationsCisco · OACisco interview questions
- Fire Station Response BillLinkedIn · ONSITE INTERVIEWLinkedIn interview questions
- Shortest Word Chain Between Two WordsFanatics · PHONE SCREEN
- Shortest Path Node Count Avoiding Broken NodesGoogle · PHONE SCREEN · ONSITE INTERVIEWGoogle interview questions
Bloomberg LP interview experiences
- Bloomberg SWE Phone Screen and Onsite: 1 Experience
- Bloomberg SWE Phone Screens with Two Teams: 1 Experience
- Bloomberg AI Interview, Hiring Level Not Reported: 2 Experiences
- Bloomberg Experienced SWE AI Coding Interviews: 2 Experiences
- Bloomberg New Grad AI-Assisted Coding Rounds: 3 Experiences
- Bloomberg New Grad SWE AI-Assisted Coding Rounds: 1 Experience