Find the Methods Remaining after Removing a Suspicious Group
Quick Overview
Remove all methods directly or indirectly invoked by a buggy method only when no outside method calls into that group. Otherwise keep every method; output order is unrestricted.
Find the Methods Remaining after Removing a Suspicious Group
Company: Google
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Other
### Problem
A project has `n` methods numbered `0` through `n - 1`. Each row `[a, b]` in `invocations` means that method `a` invokes method `b`.
Method `k` contains a known bug. It and every method it invokes, directly or indirectly, are suspicious. You want to remove exactly these suspicious methods. A group may be removed only if no method outside the group invokes a method inside it.
Return all remaining method IDs after removing the suspicious group. If that entire group cannot be removed, remove nothing and return all method IDs. The answer may be returned in any order.
```cpp
class Solution {
public:
vector<int> remainingMethods(int n, int k, vector<vector<int>>& invocations);
};
```
### Examples
**Example 1:**
```text
Input: n = 6, k = 2, invocations = [[2,3],[3,2],[3,4],[0,1],[1,5]]
Output: [0,1,5]
Explanation: Methods 2, 3, and 4 are suspicious. No other method invokes any of them, so they are removed. Any ordering of the three remaining IDs is valid.
```
**Example 2:**
```text
Input: n = 5, k = 1, invocations = [[1,2],[2,3],[4,2]]
Output: [0,1,2,3,4]
Explanation: Methods 1, 2, and 3 are suspicious, but method 4 is outside that group and invokes method 2. Nothing can be removed.
```
### Constraints
- `1 <= n <= 100000`.
- `0 <= k < n`.
- `0 <= invocations.length <= 200000`.
- Every invocation contains exactly two integers `[a, b]`.
- `0 <= a, b < n` and `a != b`.
- No two rows of `invocations` are identical.
### Clarifying Questions
- Is an invocation directed? Yes. `[a, b]` means a invokes b, not necessarily the reverse.
- Can the invocation graph contain a cycle? Yes.
- May only part of the suspicious group be removed? No. Remove all of it or remove nothing.
- May extra nonsuspicious methods be removed to make removal possible? No.
- Can the returned array be empty? Yes, if every method is suspicious and the whole group is removed.
- Is ascending output order required? No. Any ordering of the remaining IDs is valid.
### What a Strong Answer Covers
- Identifies every method included by the direct-or-indirect suspicion rule, including cyclic calls.
- Enforces the removal condition and the all-or-nothing result.
- Preserves the source's any-order output contract and handles an empty invocation list.
```hint Separate membership from permission
Model invocations as a directed graph. First find all methods reachable from k; then inspect whether an invocation enters that set from outside it.
```
### Follow-up Questions
- How does the result differ between a project with no invocations and one in which every method is suspicious?
- If a previously isolated nonsuspicious method gains an invocation into the suspicious group, how does that affect which methods remain?
Overview: Remove all methods directly or indirectly invoked by a buggy method only when no outside method calls into that group. Otherwise keep every method; output order is unrestricted.
Find the Methods Remaining after Removing a Suspicious Group
Google
Source window: 0–3 months · as of Oct 21, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0
Problem
A project has n methods numbered 0 through n - 1. Each row [a, b] in invocations means that method a invokes method b.
Method k contains a known bug. It and every method it invokes, directly or indirectly, are suspicious. You want to remove exactly these suspicious methods. A group may be removed only if no method outside the group invokes a method inside it.
Return all remaining method IDs after removing the suspicious group. If that entire group cannot be removed, remove nothing and return all method IDs. The answer may be returned in any order.
class Solution {
public:
vector<int> remainingMethods(int n, int k, vector<vector<int>>& invocations);
};
Examples
Example 1:
Input: n = 6, k = 2, invocations = [[2,3],[3,2],[3,4],[0,1],[1,5]]
Output: [0,1,5]
Explanation: Methods 2, 3, and 4 are suspicious. No other method invokes any of them, so they are removed. Any ordering of the three remaining IDs is valid.
Example 2:
Input: n = 5, k = 1, invocations = [[1,2],[2,3],[4,2]]
Output: [0,1,2,3,4]
Explanation: Methods 1, 2, and 3 are suspicious, but method 4 is outside that group and invokes method 2. Nothing can be removed.
Constraints
1 <= n <= 100000
.
0 <= k < n
.
0 <= invocations.length <= 200000
.
Every invocation contains exactly two integers
[a, b]
.
0 <= a, b < n
and
a != b
.
No two rows of
invocations
are identical.
Clarifying Questions Guidance
Is an invocation directed? Yes.
[a, b]
means a invokes b, not necessarily the reverse.
Can the invocation graph contain a cycle? Yes.
May only part of the suspicious group be removed? No. Remove all of it or remove nothing.
May extra nonsuspicious methods be removed to make removal possible? No.
Can the returned array be empty? Yes, if every method is suspicious and the whole group is removed.
Is ascending output order required? No. Any ordering of the remaining IDs is valid.
What a Strong Answer Covers Guidance
Identifies every method included by the direct-or-indirect suspicion rule, including cyclic calls.
Enforces the removal condition and the all-or-nothing result.
Preserves the source's any-order output contract and handles an empty invocation list.
Follow-up Questions Guidance
How does the result differ between a project with no invocations and one in which every method is suspicious?
If a previously isolated nonsuspicious method gains an invocation into the suspicious group, how does that affect which methods remain?