FastPrepTop-K Affinity Nodes in a Connected Component
Problems/Top-K Affinity Nodes in a Connected Component

Top-K Affinity Nodes in a Connected Component

Google logoGoogle● MediumNEW GRADONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: The report describes a connected-component task with an affinity score for each node, a BFS plus sort solution, a top-k heap optimization, and a discussion of union-find. We fixed which component is queried, that output is node IDs, the tie order, and the edge cases. Our version is about 84% the same as the reported question.

You are given an undirected graph with nodes 0 through n-1. Node i has integer affinity affinity[i].

Find all nodes you can reach from start, including start itself. Return up to k of those node IDs with the highest affinity. Put higher affinity first; for equal affinity, put the smaller node ID first.

Duplicate edges and self-loops do not change the component. Return an empty array when k == 0.

Function

topAffinityNodes(n: int, edges: int[][], affinity: int[], start: int, k: int) → int[]

Examples

Example 1

n = 6edges = [[0,1],[1,2],[3,4]]affinity = [5,9,7,100,50,8]start = 0k = 2return = [1,2]

The component is {0,1,2}; nodes 1 and 2 have its two highest affinities.

Example 2

n = 4edges = [[0,1],[1,2],[2,3]]affinity = [4,4,4,4]start = 2k = 3return = [0,1,2]

All affinities tie, so smaller node IDs come first.

Example 3

n = 3edges = []affinity = [8,2,5]start = 1k = 3return = [1]

An isolated start node is a one-node component.

Constraints

  • 1 <= n == affinity.length <= 200000.
  • 0 <= edges.length <= 300000.
  • Every edge endpoint is a valid node ID.
  • -10^9 <= affinity[i] <= 10^9.
  • 0 <= start < n and 0 <= k <= n.

More Google Onsite Interview questions

  • Route Pattern MatcherPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Evaluate a Nested Math ExpressionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Distinct Subset SumsONSITE INTERVIEW · Seen Nov 2026
  • Playlist With No Same Artist in a RowONSITE INTERVIEW · Seen Nov 2026
  • Delete Nodes and Return Forest RootsONSITE INTERVIEW · Seen Nov 2026
  • Dice-Walk Winning ProbabilityPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • First and Last Target Position in a Mountain ArrayONSITE INTERVIEW · Seen Nov 2026
  • Lowest Common Ancestor in an N-ary TreePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • 0/1 Knapsack with Weight and Volume LimitsONSITE INTERVIEW · Seen Nov 2026
  • Batched Subsequence Word MatchesONSITE INTERVIEW · Seen Nov 2026

Similar questions at other companies

  • Course Schedule IIAmazon · PHONE SCREEN · ONSITE INTERVIEW
  • Count Friend CirclesOpenAI · PHONE SCREEN · ONSITE INTERVIEWOpenAI interview questions
  • Course Schedule IIWalmart · ONSITE INTERVIEWWalmart interview questions
  • Count Connected Components in an Undirected GraphNutanix · ONSITE INTERVIEWNutanix interview questions
  • Task Order With PrerequisitesDoorDash · PHONE SCREEN · ONSITE INTERVIEWDoorDash interview questions
  • Course Schedule IIBloomberg LP · ONSITE INTERVIEWBloomberg LP interview questions
  • Find All People With SecretIDFC Bank · ONSITE INTERVIEWIDFC Bank interview questions
  • Shortest Travel Time in a Grid with Traffic LightsWaymo · PHONE SCREENWaymo interview questions
  • Record Linkage Part 3 - Full Connected ComponentStripe · PHONE SCREENStripe interview questions
  • Evaluate DivisionApple · PHONE SCREENApple interview questions

Interview reports that mention this question

  • Google New Grad Timeline to Team Match: 1 Experience

Google interview experiences

  • Google interviews · November 2026
  • Google Software Engineer Onsite Questions: 10 Experiences
  • Google OA: What It Tests (2026)
  • Google L4 SWE Phone Screen: 1 Experience
  • Google ML Engineering Manager Onsite: 1 Experience
All Google interview questionsSee Google hiring insights
Saved
class Solution {
    public int[] topAffinityNodes(int n, int[][] edges, int[] affinity, int start, int k) {
        // Write your code here.
    }
}
Checking account…