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
Coding & Algorithms
Software Engineer
Meta
November 29, 2026
Software Engineer
Phone Screen
Coding & Algorithms
Easy

0

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
directed graph
in-degree
out-degree
counting
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. 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 Problems
Sample Answer
Requirements
  • Input: n accounts labeled 1..n, and follows, a list of [a, b] pairs meaning a follows b.
  • Output: the label of the unique account followed by all n - 1 others 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...


Submit Your Answer
Markdown supported