Amount of Time to Infect a Tree
Updated
Problem statement
You are given a tree of cities. Each city has an integer name. The tree edges are given as strings in the format "u->v", meaning city u is connected to city v.
At minute 0, an infection starts at city start. Each minute, every currently infected city infects all adjacent uninfected cities.
Return the number of minutes needed for the entire tree to become infected.
Function
minutesToInfectTree(graph: String[], start: int) → intExamples
Example 1
graph = ["1->5", "1->3", "5->4", "4->9", "4->2", "3->10", "3->6"]start = 3return = 4The farthest cities from 3 are 9 and 2, each at distance 4. Therefore all cities are infected after 4 minutes.
Example 2
graph = ["1->2", "2->3", "3->4"]start = 2return = 2City 1 is infected after 1 minute, city 3 after 1 minute, and city 4 after 2 minutes.
Example 3
graph = ["1->2", "1->3", "1->4", "1->5", "1->6", "1->7", "1->8", "1->9", "1->10"]start = 5return = 2Star graph with 10 nodes, start at a leaf (node 5). Infection spreads to center (1) at minute 1, then to all other leaves at minute 2. Answer is 2.
Constraints
- The given edges form a tree.
- Each edge string has the format
"u->v". startis a city in the tree.
Source note: Author-written prompt excerpt retained for source fidelity.
More Microsoft Onsite Interview questions
- Bad Request AlertsONSITE INTERVIEW · Seen Nov 2026
- Longest Substring Without Repeating CharactersONSITE INTERVIEW · Seen Nov 2026
- Valid ParenthesesONSITE INTERVIEW · Seen Nov 2026
- Count a User's Actions in an Event LogONSITE INTERVIEW · Seen Nov 2026
- Smallest Batch Size Within a Step LimitONSITE INTERVIEW · Seen Nov 2026
- Count Subarrays with Equal Even and Odd CountsONSITE INTERVIEW · Seen Nov 2026
- Minimum Time to Rot All OrangesONSITE INTERVIEW · Seen Nov 2026
- Coin ChangeONSITE INTERVIEW · Seen Oct 2026
- Daily USD Totals with Effective-Dated Exchange RatesONSITE INTERVIEW · Seen Oct 2026
- Number Words to Digits in Any LanguageONSITE INTERVIEW · Seen Oct 2026
Similar questions at other companies
- Course ScheduleAmazon · PHONE SCREEN · ONSITE INTERVIEW
- Course ScheduleOracle · ONSITE INTERVIEWOracle interview questions
- 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
- Evaluate DivisionPhonePe · ONSITE INTERVIEWPhonePe interview questions
- Count Friend CirclesOpenAI · PHONE SCREEN · ONSITE INTERVIEWOpenAI interview questions
- Finite Web CrawlerMongoDB · PHONE SCREEN · ONSITE INTERVIEWMongoDB interview questions
Microsoft interview experiences
- Microsoft OA Questions, October 2026: 4 Experiences
- Microsoft Experienced SWE Phone Screen: 1 Experience
- Microsoft Explore and Student SWE: 12 Experiences (Part 6 of 6)
- Microsoft SWE Intern, Early 2025: 41 Experiences (Part 5 of 6)
- Microsoft SWE Intern, Mid 2025: 42 Experiences (Part 4 of 6)
- Microsoft SWE Intern, Late 2025: 47 Experiences (Part 3 of 6)