Find Friends by Degree
Updated
Problem statement
There are n people with IDs 0..n-1. Each pair [u,v] in edges represents an undirected friendship. For a person start, return their friends grouped by degree through degrees.
For this exercise, assume degree means the shortest number of friendship edges from start. Return exactly degrees rows: row d-1 contains every person at distance d, sorted by ID. Include empty rows for missing degrees. Exclude start and unreachable people; list each person only once. If degrees=0, return []. Repeated edge entries represent the same friendship.
Function
friendsByDegree(n: int, edges: int[][], start: int, degrees: int) → int[][]Examples
Example 1
n = 7edges = [[0,1],[0,2],[1,3],[2,3],[3,4],[5,6]]start = 0degrees = 4return = [[1,2],[3],[4],[]]People 1 and 2 are direct friends, 3 is two steps away, and 4 is three steps away. The fourth row is empty. People 5 and 6 are unreachable from 0.
Example 2
n = 4edges = [[2,3],[0,1],[1,2]]start = 2degrees = 2return = [[1,3],[0]]From person 2, people 1 and 3 are one edge away; person 0 requires two edges. Each row is sorted by ID.
Constraints
- For this exercise, assume 1 <= n <= 500.
- For this exercise, assume 0 <= edges.length <= 2000.
- Each edge has exactly two integer IDs, with 0 <= u < v < n.
- 0 <= start < n.
- For this exercise, assume 0 <= degrees <= 500.
More Morgan Stanley Onsite Interview questions
- Generate Strings of Length at Least ThreeONSITE INTERVIEW · Seen Sep 2026
- Maximum Concurrent ProcessesOA · ONSITE INTERVIEW · Seen Sep 2026
- Kth Largest Array ElementONSITE INTERVIEW · Seen Jul 2026
- Run-Length Encode Consecutive Letter RunsONSITE INTERVIEW · Seen Jul 2026
- Maximum Edge Removals for Two TravelersONSITE INTERVIEW · Seen Oct 2025
- Remove K DigitsONSITE INTERVIEW · Seen Oct 2025
- Count Ideal ArraysONSITE INTERVIEW · Seen Aug 2023
- Distinct Palindrome PartitionsONSITE INTERVIEW · Seen Aug 2023
- LFU Cache OperationsONSITE INTERVIEW · Seen Aug 2023
- All-Pairs Shortest PathsOA · ONSITE INTERVIEW · Seen Feb 2022
Similar questions at other companies
- Number of IslandsAmazon · PHONE SCREEN · ONSITE INTERVIEW
- Course SchedulePlaud · ONSITE INTERVIEW
- Explore an Unknown Grid and Find a PathLinkedIn · ONSITE INTERVIEWLinkedIn interview questions
- Nearest Charging Station on a GridSpotify · ONSITE INTERVIEW
- Hierarchical Permission CheckeBay · ONSITE INTERVIEWeBay interview questions
- Shortest Path Node Count Avoiding Broken NodesGoogle · PHONE SCREEN · ONSITE INTERVIEWGoogle interview questions
- Island Count and Maximum AreaOracle · ONSITE INTERVIEWOracle interview questions
- Evaluate DivisionPhonePe · ONSITE INTERVIEWPhonePe interview questions
- Count Friend CirclesOpenAI · PHONE SCREEN · ONSITE INTERVIEWOpenAI interview questions
- Finite Web CrawlerMongoDB · PHONE SCREEN · ONSITE INTERVIEWMongoDB interview questions
Interview reports that mention this question
Morgan Stanley interview experiences
- Morgan Stanley Apprenticeship and Tech Interviews: 6 Experiences
- Morgan Stanley New Grad Technology Analyst: 2 Experiences
- Morgan Stanley Intern Oldest Loops: 5 Experiences (Part 5 of 5)
- Morgan Stanley Intern OOP Loops: 5 Experiences (Part 4 of 5)
- Morgan Stanley Intern Tech Rounds: 5 Experiences (Part 2 of 5)