FastPrepFind the Celebrity from Pairwise Acquaintances
Problems/Find the Celebrity from Pairwise Acquaintances

Find the Celebrity from Pairwise Acquaintances

Salesforce logoSalesforce● MediumFULLTIMEONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported in a Salesforce onsite interview. The report describes the well-known celebrity problem, where you can ask whether one person knows another. Here the question is answered by a matrix, so you can test the task as one input. We also set the usual rule for the celebrity and what to return when there is none. Our version is about 91% the same as the reported question.

A group contains n people numbered from 0 to n - 1. The matrix entry knows[a][b] is 1 exactly when person a knows person b.

A celebrity is known by every other person but knows no other person. At most one celebrity exists. Return the celebrity's index, or -1 if the group has no celebrity. Diagonal entries are irrelevant.

Function

findCelebrity(knows: int[][]) → int

Examples

Example 1

knows = [[0,1,0],[0,0,0],[0,1,0]]return = 1

People 0 and 2 know person 1, while person 1 knows neither of them.

Example 2

knows = [[0,1],[1,0]]return = -1

Each person knows the other, so neither can be a celebrity.

Example 3

knows = [[1]]return = 0

With one person, all off-diagonal celebrity conditions hold vacuously.

Constraints

  • 1 <= n <= 2000.
  • knows is an n x n matrix.
  • Every entry is 0 or 1.
  • Only off-diagonal entries determine the result.

More Salesforce Onsite Interview questions

  • Count Increasing TripletsONSITE INTERVIEW · Seen Nov 2026
  • Longest Substring Without Repeating CharactersONSITE INTERVIEW · Seen Nov 2026
  • Reverse a Sublist of a Linked ListONSITE INTERVIEW · Seen Nov 2026
  • Rotting OrangesONSITE INTERVIEW · Seen Nov 2026
  • 3Sum SmallerONSITE INTERVIEW · Seen Nov 2026
  • Count Zero Request ServersONSITE INTERVIEW · Seen Nov 2026
  • Group Identical Tree LevelsONSITE INTERVIEW · Seen Oct 2026
  • Meeting Rooms IIONSITE INTERVIEW · Seen Oct 2026
  • Construct a Tree from Level-Order and Inorder TraversalsONSITE INTERVIEW · Seen Sep 2026
  • Maximize Monsters DefeatedONSITE INTERVIEW · Seen Sep 2026

Similar questions at other companies

  • Find the Celebrity from Pairwise AcquaintancesAmazon · ONSITE INTERVIEW
  • Minimum Operations to Sort with Rotations Then SwapsBank of New York Mellon · OA
  • Optimal TransferSnowflake · OASnowflake interview questions
  • Maximum XOR Elimination ScoreRippling · OARippling interview questions
  • Minimum Piano Hand Raises and Thumb PositionsGoogle · ONSITE INTERVIEWGoogle interview questions
  • Fair Capped Money DistributionGusto · PHONE SCREEN · ONSITE INTERVIEWGusto interview questions
  • Mutual-Friend RecommendationsMeta · PHONE SCREEN · ONSITE INTERVIEWMeta interview questions
  • Tennis Court SchedulingAtlassian · PHONE SCREEN · ONSITE INTERVIEWAtlassian interview questions
  • Minimum Station Tracks for a Train ScheduleBNSF Railway · ONSITE INTERVIEWBNSF Railway interview questions
  • Best Time to Buy and Sell Stock IIKotak Mahindra Bank · ONSITE INTERVIEWKotak Mahindra Bank interview questions

Salesforce interview experiences

  • Salesforce Infrastructure SWE OA and Onsite: 1 Experience
  • Salesforce Experienced Backend SWE First Round: 1 Experience
All Salesforce interview questionsSee Salesforce hiring insights
Saved
class Solution {
    public int findCelebrity(int[][] knows) {
      // write your code here
    }
}
Checking account…