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
Remove Methods From Project - DFS Approach INPUT Method Call Graph 0 1 BUGGY (k) 2 infected 3 n=4, k=1 invocations=[[1,2],[0,1],[3,2]] ALGORITHM STEPS 1 Find Suspicious DFS from k=1: find all reachable methods {1,2} 2 Check External Calls Check if non-suspicious calls into suspicious set 3 Validate Removal 3 calls 2 (suspicious) External call found! 4 Decision Can't remove {1,2} safely Keep all OR remove none DFS Traversal from k=1 1 --> 2 Suspicious set: {1, 2} Safe set: {0, 3} FINAL RESULT Methods After Analysis 0 KEPT 3 KEPT 1 2 Suspicious methods removed Output: [0, 3] Key Insight: We can only remove suspicious methods if NO external method calls into them. Here, method 3 (non-suspicious) calls method 2 (suspicious via [3,2]). Since 3 is not in the suspicious set but calls into it, we cannot safely remove the buggy group. DFS helps find all methods reachable from k, then we verify no external dependencies. TutorialsPoint - Remove Methods From Project | DFS Approach
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