Find the Account Everyone Follows but Who Follows No One
Last updated: November 29, 2026
Quick Overview
Given n Instagram accounts labeled 1 to n and a list of directed follow pairs [a, b] meaning a follows b, return the account that every other account follows and that follows nobody, or -1 if no such account exists.
Meta
November 29, 20260
0
0 solved
Given n Instagram accounts labeled 1 to n and a list of directed follow pairs [a, b] meaning a follows b, return the account that every other account follows and that follows nobody, or -1 if no such account exists.
A warm-up in the style of a Meta phone screen, framed around the Instagram follow graph. The interviewer checks that you reduce it to in-degree and out-degree counting instead of building adjacency sets, and that you finish quickly enough to leave time for a second problem.
What the Interviewer Expects
- Recognize the problem as a degree-counting check on a directed graph
- Use a single score array (in minus out) instead of two maps
- Handle n = 1 and duplicate follow pairs correctly
- Run in O(n + e) time and O(n) space
- Explain why at most one account can qualify
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- How would you solve it with O(1) extra space if you could only query follows(a, b)?
- What changes if duplicate follow pairs appear in the input?
- How would you find every account followed by at least 90 percent of users at Meta scale?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
- Input:
naccounts labeled1..n, andfollows, a list of[a, b]pairs meaningafollowsb. - Output: the label of the unique account followed by all
n - 1others that follows nobody, els...
High-Level Design
This is a degree problem on a directed graph. The target account has in-degree n - 1 and out-degree 0. I keep one integer per account: add one for each incoming edge, subtract one for each outgoin...