Gossip Consensus on the Maximum Value

Reported by candidates from Bloomberg's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live Bloomberg OA. Under 2s to a working solution.
Founder's read

Bloomberg reported this one in July 2022, and the trap is hiding in plain sight: the answer isn't the graph's diameter. If you've got an OA invite and you're picturing Floyd-Warshall on 10^5 nodes, stop. This is a gossip problem on a connected graph, and the rounds depend on where the maximum lives. Multiple nodes can tie for the max, and a single node can be the whole graph. Get those wrong and the visible example still passes. StealthCoder sits invisibly as a safety net if you blank on the live OA, but the idea is short enough to hold in your head.

The problem

Node i starts knowing values[i]. edges describes a connected undirected network. In one synchronous round, each node replaces its known value with the maximum value known at the start of that round by itself and all of its neighbors.
Return [globalMaximum, roundsUntilEveryNodeKnowsIt].

Function
gossipMaximum(values: int[], edges: int[][]) → int[]

Examples
Example 1
values = [3,9,2,5]
edges = [[0,1],[1,2],[2,3]]
return = [9,2]
The maximum starts at node 1 and reaches node 3 after two synchronous rounds.

Constraints
1 <= values.length <= 10^5.
The graph is connected.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The global maximum is just max(values). Every round, the max spreads one hop outward from each node holding it. So the number of rounds is the largest shortest-path distance from the nearest max-holder to any node. That's a multi-source BFS: push every node whose value equals the max into the queue at distance 0, run BFS, and return the biggest distance reached. The pitfall is picking one max node and running a single-source BFS. With ties, that overcounts rounds. Another trap is using the diameter, which is wrong whenever the max sits near the center. A single node gives [value, 0], since no rounds are needed. Build an adjacency list, not a matrix, because 10^5 nodes kills O(n^2) memory. Complexity is O(n + e). If the edge case slips past you mid-assessment, StealthCoder is the hedge that catches it live.

Drill it cold or hedge it with StealthCoder. Either way, don't walk into the OA hoping you remember the trick.

If this hits your live OA

You can drill Gossip Consensus on the Maximum Value cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Made for the candidate who got the OA invite this morning and has 72 hours, not six months.

Get StealthCoder

Related leaked OAs

⏵ The honest play

You've seen the question. Make sure you actually pass Bloomberg's OA.

Bloomberg reuses patterns across OAs. Made for the candidate who got the OA invite this morning and has 72 hours, not six months. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Gossip Consensus on the Maximum Value FAQ

What's the trick in the Bloomberg gossip maximum problem?+

Treat it as multi-source BFS. Seed the queue with every node holding the global maximum at distance 0. The rounds needed equal the largest BFS distance to any node. It's not the diameter and not a single-source BFS from one arbitrary max node.

What happens when several nodes tie for the maximum?+

All of them start spreading at the same time, so they all go in the queue at distance 0. Picking only one overcounts the rounds. This tie case is the edge that breaks the naive solution, and the sample doesn't show it.

What should the answer be for a single node?+

Return [values[0], 0]. That node already knows the maximum, so zero rounds are needed. Your BFS handles it naturally if you initialize distances correctly, but test it explicitly, since length 1 is allowed by the constraints.

How hard is this really?+

Easy to medium. The code is a standard BFS with an adjacency list. The difficulty is recognizing that the rounds are the farthest distance from the nearest max-holder. Once you see that, it's about fifteen lines and O(n + e) time.

How do I prepare in 48 hours?+

Write multi-source BFS from scratch twice. Practice building adjacency lists from edge arrays and tracking distance per node. Then test three cases: a single node, tied maxima, and the max at a path's end. That covers nearly everything this problem can throw at you.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with Bloomberg.

OA at Bloomberg?
Invisible during screen share
Get it