Maximum-Sum Star with at Most K Arms
Company: C3 AI
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Online Assessment
Given a simple undirected graph with an integer value on each node and an integer `k`, return the largest node-value sum of any nonempty star subgraph with at most `k` arms.
A star has one center and zero or more distinct neighbors selected as leaves. Each selected leaf must be joined to the center by a graph edge. A center alone is a valid star. Count each selected node's value once. The chosen star is a subgraph: any edges between selected leaves need not be included.
### Function and Inputs
Complete `bestSumKStar(g_nodes, g_from, g_to, values, k)`:
- `g_nodes` is the number of nodes, labeled `0` through `g_nodes - 1`.
- `g_from` and `g_to` are equally sized arrays. Edge `i` joins `g_from[i]` and `g_to[i]`.
- `values[i]` is the integer value of node `i`.
- `k` is the maximum number of arms in the star.
Return one integer: the maximum sum among all allowed stars.
### Constraints
- `2 <= g_nodes <= 100000` and `1 <= g_edges <= 100000`, where `g_edges` is the length of each edge array.
- Every edge endpoint is a valid node label. The graph has no self-loops and at most one edge between a pair of nodes.
- `values` has length `g_nodes`, and `-1000 <= values[i] <= 1000`.
- `0 <= k <= 100000`. The graph need not be connected.
### Example 1
```text
g_nodes = 4
g_from = [0, 0, 0]
g_to = [1, 2, 3]
values = [5, 4, -2, 3]
k = 2
answer = 12
```
The star centered at node `0` with leaves `1` and `3` has sum `5 + 4 + 3 = 12`.
### Example 2
```text
g_nodes = 2
g_from = [0]
g_to = [1]
values = [-4, -1]
k = 1
answer = -1
```
The valid star containing only node `1` has sum `-1`.
Overview: Find the maximum node-value sum of a graph star with at most k arms, allowing a center alone and avoiding harmful negative-value leaves.
Read the full C3 AI Software Engineer interview experience this question came from
Given a simple undirected graph with an integer value on each node and an integer `k`, return the largest node-value sum of any nonempty star subgraph with at most `k` arms.
A star has one center and zero or more distinct neighbors selected as leaves. Each selected leaf must be joined to the center by a graph edge. A center alone is a valid star. Count each selected node's value once. The chosen star is a subgraph: any edges between selected leaves need not be included.
### Function and Inputs
Complete `bestSumKStar(g_nodes, g_from, g_to, values, k)`:
- `g_nodes` is the number of nodes, labeled `0` through `g_nodes - 1`.
- `g_from` and `g_to` are equally sized arrays. Edge `i` joins `g_from[i]` and `g_to[i]`.
- `values[i]` is the integer value of node `i`.
- `k` is the maximum number of arms in the star.
Return one integer: the maximum sum among all allowed stars.
### Constraints
- `2 <= g_nodes <= 100000` and `1 <= g_edges <= 100000`, where `g_edges` is the length of each edge array.
- Every edge endpoint is a valid node label. The graph has no self-loops and at most one edge between a pair of nodes.
- `values` has length `g_nodes`, and `-1000 <= values[i] <= 1000`.
- `0 <= k <= 100000`. The graph need not be connected.
### Example 1
```text
g_nodes = 4
g_from = [0, 0, 0]
g_to = [1, 2, 3]
values = [5, 4, -2, 3]
k = 2
answer = 12
```
The star centered at node `0` with leaves `1` and `3` has sum `5 + 4 + 3 = 12`.
### Example 2
```text
g_nodes = 2
g_from = [0]
g_to = [1]
values = [-4, -1]
k = 1
answer = -1
```
The valid star containing only node `1` has sum `-1`.
Constraints
- 2 <= g_nodes <= 100000 and nodes are labeled 0 through g_nodes - 1.
- g_from and g_to have the same length g_edges with 1 <= g_edges <= 100000; endpoints are valid, with no self-loops or duplicate undirected edges.
- values has length g_nodes and each value is an integer from -1000 through 1000.
- 0 <= k <= 100000; the graph may be disconnected.
- A center alone is valid, and edges among selected leaves need not be included.
Examples
Input: (4, [0, 0, 0], [1, 2, 3], [5, 4, -2, 3], 2)
Expected Output: 12
Explanation: The center 0 uses its two best positive neighbors, 1 and 3.
Input: (2, [0], [1], [-4, -1], 1)
Expected Output: -1
Explanation: Choosing node 1 alone avoids a harmful negative leaf.
Hints
- A center with no selected leaves is a valid star.
- Selecting fewer than k neighbors is allowed.