FastPrepMaximum K-Star Sum
Problems/Maximum K-Star Sum

Maximum K-Star Sum

C3.ai logoC3.ai● MediumNEW GRADOA

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Our version is about 99% the same as the reported question.

You are given an undirected graph with g_nodes vertices, numbered from 0 to g_nodes - 1. Each vertex has an integer value. You are also given a nonnegative integer k. Find the largest sum of a nonempty k-star.

A k-star is one center vertex plus at most k different neighbors of that center. A neighbor is a vertex joined to the center by an edge. Each chosen neighbor is one arm. A center with no arms is allowed. The sum of a star is the center's value plus the values of all chosen neighbors.

The edges come in two arrays of the same length, g_from and g_to. Edge i joins g_from[i] and g_to[i]. Vertex i has value values[i].

Return the largest sum over all choices of center and arms. No edge joins a vertex to itself, and no edge is repeated. Two chosen neighbors may also be joined by an edge. That does not stop you from choosing both.

Function

bestSumKStar(g_nodes: int, g_from: int[], g_to: int[], values: int[], k: int) → int

Examples

Example 1

g_nodes = 7g_from = [0,0,0,3,3,3]g_to = [1,2,3,4,5,6]values = [1,-2,3,4,5,-1,-2]k = 2return = 10

Use vertex 3 as the center and vertices 4 and 0 as arms. The sum is 4 + 5 + 1 = 10. You do not have to pick the negative neighbors.

Example 2

g_nodes = 2g_from = [0]g_to = [1]values = [-5,-2]k = 0return = -2

No arms are allowed, so each star is a single vertex. Vertex 1, with value -2, is the better choice. An empty star with sum 0 is not allowed.

Example 3

g_nodes = 3g_from = [0,1,0]g_to = [1,2,2]values = [-10,4,9]k = 3return = 13

Choose vertex 2 as the center and its positive neighbor 1 as an arm. The sum is 13. You may use fewer than k arms.

Constraints

  • 2 <= g_nodes <= 10^5.
  • 1 <= g_from.length = g_to.length <= 10^5.
  • 0 <= g_from[i], g_to[i] < g_nodes.
  • values.length = g_nodes and -10^3 <= values[i] <= 10^3.
  • 0 <= k <= 10^5.
  • The graph is undirected. No edge joins a vertex to itself, and no edge is repeated.

More C3.ai interview questions

  • Drop Recent Duplicate MessagesOA · Seen Oct 2026
  • Largest Square Sub-Grid Within a Sum LimitOA · Seen Jan 2023
  • Prefix Scores With the Running MaximumOA · Seen Jan 2023
  • Tree Node With Maximum BeautyOA · Seen Nov 2022
  • Count Vowel Strings With Follow RulesOA · Seen Apr 2022
  • Photo Album Insert OrderOA · Seen Apr 2022
  • Sort by Frequency, Then ValueOA · Seen Apr 2022
  • Count Distinct Binary Search TreesOA · Seen Mar 2022
  • Count Palindromic Subsequences of Length KOA · Seen Mar 2022
  • Fewest Turns to Open a Wheel LockPHONE SCREEN · Seen Nov 2024

Similar questions at other companies

  • Maximum Sum of a K-StarMicrosoft · OAMicrosoft interview questions
  • Google Drive Folder Hierarchy SyncGoogle · OAGoogle interview questions
  • Water Supply With Wells and PipesNutanix · ONSITE INTERVIEWNutanix interview questions
  • Minimum Cost to Merge Values into OnePayPal · OAPayPal interview questions
  • Task SchedulerAmazon · OA · PHONE SCREEN · ONSITE INTERVIEW
  • Minimum Operations to Sort with Rotations Then SwapsBank of New York Mellon · OA
  • Maximum ScoreNVIDIA · OANVIDIA interview questions
  • Maximize Revenue from Decreasing InventoryChubb · OAChubb interview questions
  • Optimal TransferSnowflake · OASnowflake interview questions
  • Task SchedulingJP Morgan Chase · OAJP Morgan Chase interview questions

C3.ai interview experiences

  • C3.ai FDE and Other New Grad Roles: 5 Experiences (Part 2 of 2)
  • C3.ai New Grad Interviews: 25 Experiences (Part 1 of 2)
  • C3.ai New Grad Forward Deployed Engineer Interview: 1 Experience
All C3.ai interview questionsSee C3.ai hiring insights
Saved
class Solution {
    public int bestSumKStar(int g_nodes, int[] g_from, int[] g_to, int[] values, int k) {
        // write your code here
    }
}
Checking account…