Find spammer with minimal hasMessaged calls
Problem Description
You are given n accounts, labeled from 0 to n-1. An oracle function, hasMessaged(a, b), is available, which returns true if account a has sent at least one message to account b, and false otherwise.
A "spammer" is defined as an account that satisfies two conditions:
- It has sent at least one message to every other account (i.e., all accounts
jwherej != a). - It has received messages from no other account (i.e., for all accounts
jwherej != a,hasMessaged(j, a)isfalse).
Your task is to find and return the label of a spammer account if one exists. If no spammer is found, return -1.
Constraints and Deliverables
Your solution must adhere to the following complexity constraints:
- Time Complexity:
O(n)calls to thehasMessagedoracle. - Space Complexity:
O(1)extra space.
You are required to:
- Provide the algorithm.
- Prove its correctness.
- Analyze its time and space complexity.
- Discuss edge cases, specifically:
nequals0or1.- Scenarios involving multiple potential spammer candidates.
- The implications of self-messages (i.e.,
hasMessaged(a, a)).
AI interview prep
Core dilemma
Efficiently identifying a unique node with specific in-degree and out-degree properties in a directed graph, given an extremely constrained query budget (O(N) oracle calls) and minimal memory (O(1) extra space).
2 tradeoffs and 3 stress tests locked
Interview prep
Unlock the interviewer X-Ray rubric, tradeoff matrix, stress tests, clarifying questions, and key concepts.
Find spammer with minimal hasMessaged calls
Problem Description
You are given n accounts, labeled from 0 to n-1. An oracle function, hasMessaged(a, b), is available, which returns true if account a has sent at least one message to account b, and false otherwise.
A "spammer" is defined as an account that satisfies two conditions:
- It has sent at least one message to every other account (i.e., all accounts
jwherej != a). - It has received messages from no other account (i.e., for all accounts
jwherej != a,hasMessaged(j, a)isfalse).
Your task is to find and return the label of a spammer account if one exists. If no spammer is found, return -1.
Constraints and Deliverables
Your solution must adhere to the following complexity constraints:
- Time Complexity:
O(n)calls to thehasMessagedoracle. - Space Complexity:
O(1)extra space.
You are required to:
- Provide the algorithm.
- Prove its correctness.
- Analyze its time and space complexity.
- Discuss edge cases, specifically:
nequals0or1.- Scenarios involving multiple potential spammer candidates.
- The implications of self-messages (i.e.,
hasMessaged(a, a)).