Find Min Possible Diameter
Reported by candidates from Google's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Google's May 2024 OA reportedly includes a tree problem where n goes up to 1e5, so anything quadratic is dead on arrival. You get a tree, you can delete at most k leaves one at a time, and you want the smallest diameter left. It looks like a search over deletion orders, but it isn't. It's a tree problem with a clean structural answer, and the trick is seeing what the final tree actually looks like. If you blank on it during the live assessment, StealthCoder can sit invisibly on your screen as a safety net and hand you the approach.
The problem
You are given a tree consisting of n vertices. You can perform the following operation at most k times: delete a single leaf of the tree (the operation can produce new leaves, that can be deleted later). The resulting tree must have as small diameter as possible. Find the minimum possible diameter. Input Format The first line contains two space separated integers n and k. Each of the next n-1 lines contains two space separated integers, describing the current tree edge. It's guaranteed that the given graph is a tree. Constraints 0 < n <= 1e5 0 < k < n Function findMinimumDiameter(n: int, k: int, edges: int[][]) → int Examples Example 1 n = 4 k = 0 edges = [[1, 2], [2, 3], [4, 3]] return = 3 :3 Example 2 n = 4 k = 1 edges = [[2, 3], [4, 3], [1, 4]] return = 2 :o Constraints 1 <= n <= 10^5 0 <= k < n edges.length == n - 1 Vertices are numbered from 1 to n, and edges forms a tree.
Reported by candidates. Source: FastPrep
Pattern and pitfall
Think about what survives. Deleting leaves repeatedly always leaves a connected subtree, and you remove exactly the vertices outside it, up to k of them. So the question becomes: keep at least n-k vertices as a connected subtree with minimum diameter. Any connected subtree can be reached by peeling leaves, so reachability isn't the constraint. Now the diameter is tied to a center. Pick a center vertex (or edge), then keep every vertex within distance r. Count vertices within radius r with BFS and find the smallest r where the count is at least n-k. A fixed center needs O(n) BFS, so trying all centers is O(n^2). Binary search on the diameter D and test feasibility with a multi-center count, or use the tree's own center as the best anchor. Pitfall: don't simulate deletions, and watch odd versus even diameters. StealthCoder is the hedge if the center argument escapes you mid-OA.
The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.
You can drill Find Min Possible Diameter cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Google's OA.
Google reuses patterns across OAs. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Find Min Possible Diameter FAQ
How hard is the Google Find Min Possible Diameter problem really?+
Hard if you try to simulate deletions, medium once you see it as keeping a connected subtree of at least n-k vertices. The difficulty is the reframing, not the code. Tree BFS and a counting check are standard tools.
What's the trick to avoid brute force here?+
Stop thinking about deletion order. Any connected subtree can be produced by peeling leaves, so you just need the smallest-diameter connected piece with at least n-k vertices. Anchor it on a center and count vertices within a radius.
What complexity does the OA expect with n up to 1e5?+
Roughly O(n log n) or O(n). Anything O(n^2) over all centers will time out at 1e5. Use a binary search on the answer with a linear feasibility check, or a smarter center-based counting pass.
Do I need to handle odd and even diameters separately?+
Yes. An even diameter has a single vertex center with radius D/2. An odd diameter has an edge center. Test both cases or you'll be off by one on examples like the 4-node path with k=0 returning 3.
How do I prepare for this in 48 hours?+
Practice tree diameter via two BFS passes, BFS distance counting, and binary search on the answer. Then write this one from scratch on the two given examples. Edge cases to check: k=0, which returns the original diameter, and a star-shaped tree.