FastPrepFirst Eligible Partner in a Social Graph

First Eligible Partner in a Social Graph

Bloomberg LP logoBloomberg LP● MediumNEW GRADPHONE SCREEN
Learn

Problem statement

You are given an undirected social graph with people numbered from 0 through n - 1. Each row [u, v] in edges is one social link. The array eligible has length n, where eligible[i] == 1 means person i satisfies the partner condition.

Starting from start, return the eligible person reachable with the fewest social hops. Do not consider start as their own partner. If several eligible people are equally close, return the smaller person ID. Return -1 when no eligible partner is reachable.

Function

findFirstEligiblePartner(n: int, edges: int[][], start: int, eligible: int[]) → int

Examples

Example 1

n = 6edges = [[0,1],[0,2],[1,3],[2,4],[3,5]]start = 0eligible = [0,0,0,1,1,1]return = 3

People 3 and 4 are both two hops away. The smaller ID, 3, wins the tie.

Example 2

n = 4edges = [[0,1]]start = 0eligible = [1,0,1,0]return = -1

The starting person is excluded, and the only other eligible person is disconnected.

Example 3

n = 5edges = [[0,4],[0,2],[4,3]]start = 0eligible = [0,0,1,0,1]return = 2

People 2 and 4 are each one hop away, so the smaller ID is returned.

Constraints

  • 1 <= n <= 100000.
  • 0 <= edges.length <= 200000.
  • Every edge contains two different valid person IDs; edges are unique and represent undirected links.
  • eligible.length == n and every value is 0 or 1.
  • 0 <= start < n.

More Bloomberg LP problems

See Bloomberg LP hiring insights
public int findFirstEligiblePartner(int n, int[][] edges, int start, int[] eligible) {
    // Write your code here.
}
n6
edges[[0,1],[0,2],[1,3],[2,4],[3,5]]
start0
eligible[0,0,0,1,1,1]
expected3
Checking account…