← Questions

Decaying power levels across a graph

0
GoogleCodingLast asked Feb 2026

A coding interview question asked in Google's onsite coding round, last asked Feb 2026. It tests graphs and bfs.

Some nodes of an undirected graph are power sources, each with a starting level. Power weakens by 1 for every edge it travels and never drops below 0. Each node's power level is the strongest power that reaches it from any source. Return the level of every node.

Interface

Unless you agree on a different interface with the interviewer, assume:

  • power_levels(n: int, edges: list[tuple[int, int]], sources: dict[int, int]) -> list[int] — nodes are 0..n-1; sources maps a node to its starting level. A node's level is the maximum over sources of max(0, level - distance).

Example

n = 6, edges = [(0,1), (1,2), (2,3), (3,4), (4,5)], sources = {0: 3, 5: 4}
    ->  [3, 2, 1, 2, 3, 4]
# node 2: 3 - 2 = 1 from node 0, 4 - 3 = 1 from node 5
# node 3: 3 - 3 = 0 from node 0, 4 - 2 = 2 from node 5

Constraints

  • Up to 10^5 nodes and edges, and up to n sources.
  • Levels are non-negative integers up to 10^9.

Clarifying questions

  • Does power drop by a fixed 1 per edge, or does each edge have its own decay?
  • When two sources reach a node, do they combine, or does the stronger win?
  • Is the graph directed? Can it be disconnected?

What a strong answer covers

  • Why one BFS per source is too slow: O(S · (V + E)).
  • A multi-source search started from all sources at once, processed strongest first (a max-heap, or buckets by level since levels fall by 1); a node is final the first time it is popped, and a weaker arrival is skipped.
  • Stopping propagation at level 0.
  • O((V + E) log V) with a heap, or O(V + E + max level) with buckets.
  • Tests: overlapping sources, a source inside another source's reach with a lower level, disconnected nodes.

Follow-ups

  • Each edge has its own decay amount: this is Dijkstra on the maximum remaining power.
  • Power multiplies by a factor below 1 per edge instead of subtracting.
  • Sources switch on and off over time: update levels without recomputing everything.

Where it's asked

  • Google · Onsite coding · Last asked Feb 202645 min · Usually two rounds, mostly in person: whiteboard plus a Chromebook with Google Docs · No AI (a pilot code-comprehension round lets candidates use Gemini)

What it tests

graphs · bfs

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding