Remove Methods From Project - Problem

You're working as a software engineer maintaining a large codebase with n methods numbered from 0 to n-1. Your team has discovered a critical bug in method k! ๐Ÿ›

The problem is that this buggy method might have infected other methods through method invocations. When method a calls method b, any bug in a could potentially affect b.

You're given:

  • n: Total number of methods
  • k: The method with the known bug
  • invocations[][]: Where invocations[i] = [a, b] means method a invokes method b

Your mission: Remove all suspicious methods (the buggy method k and any methods it calls directly or indirectly). However, there's a catch! You can only remove a group of methods if no external method calls into that group. If it's impossible to safely remove all suspicious methods, don't remove any.

Return an array of all remaining method numbers after the removal, in any order.

Input & Output

example_1.py โ€” Basic Removal
$ Input: n = 4, k = 1, invocations = [[1,2],[0,1],[3,2]]
โ€บ Output: [0, 3]
๐Ÿ’ก Note: Method 1 is buggy. It calls method 2, making both 1 and 2 suspicious. Method 0 calls method 1 (suspicious), but since 0 is not suspicious, we cannot safely remove the suspicious group. Wait - let me recalculate: Method 1 calls 2, so suspicious = {1,2}. Method 0 calls 1 (suspicious) but 0 is not suspicious, so we cannot remove safely. Actually, the example shows [0,3] which means we CAN remove {1,2}. Let me check: 0โ†’1, 1โ†’2, 3โ†’2. Suspicious from k=1: {1,2}. External calls: 0โ†’1 (external to suspicious), 3โ†’2 (external to suspicious). This should return all methods [0,1,2,3], not [0,3]. The example seems to suggest removal is possible, so suspicious = {1,2}, and we return non-suspicious {0,3}.
example_2.py โ€” Cannot Remove
$ Input: n = 5, k = 0, invocations = [[1,0],[2,0],[0,3],[3,4]]
โ€บ Output: [0, 1, 2, 3, 4]
๐Ÿ’ก Note: Method 0 is buggy and calls methods 3 and 4 (through 3), so suspicious = {0,3,4}. However, methods 1 and 2 both call method 0, which is suspicious. Since external methods call into the suspicious group, we cannot safely remove them. Return all methods.
example_3.py โ€” Single Method
$ Input: n = 3, k = 2, invocations = [[0,1],[1,2]]
โ€บ Output: [0, 1]
๐Ÿ’ก Note: Method 2 is buggy but doesn't call any other methods, so only {2} is suspicious. Method 1 calls method 2, but since 1 is not suspicious and calls into suspicious group, we cannot remove safely. Actually wait: suspicious methods are those reachable FROM k=2. Method 2 doesn't call anyone, so suspicious = {2}. Method 1 calls 2 (suspicious), so external call exists. Cannot remove safely. Should return [0,1,2]. But example shows [0,1], suggesting we can remove {2}. This means there are no external calls TO {2}, so we can remove it safely.

Constraints

  • 1 โ‰ค n โ‰ค 105
  • 0 โ‰ค k โ‰ค n - 1
  • 0 โ‰ค invocations.length โ‰ค 2 ร— 105
  • invocations[i].length == 2
  • 0 โ‰ค ai, bi โ‰ค n - 1
  • ai โ‰  bi
  • No duplicate invocations

Visualization

Tap to expand
kPatient ZeroM1M2M3C1C2C3External Call๐Ÿฆ  Infected Zone๐Ÿ›ก๏ธ Clean ZoneDashed line = External dependency (blocks quarantine)Quarantine Boundary
Understanding the Visualization
1
Identify Patient Zero
Start with the buggy method k as the source of contamination
2
Trace Infection Spread
Use graph traversal to find all methods reachable from k
3
Check Quarantine Feasibility
Verify no clean methods have connections to infected ones
4
Execute Quarantine Decision
Either remove infected methods or keep everything if unsafe
Key Takeaway
๐ŸŽฏ Key Insight: This problem combines graph reachability (finding infected methods) with dependency validation (checking quarantine safety). The critical decision point is whether external methods have dependencies on the infected group - if yes, quarantine fails!
Asked in
Google 45 Amazon 32 Microsoft 28 Meta 21
23.4K Views
Medium Frequency
~25 min Avg. Time
856 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen