Magicsheet logo

Largest Color Value in a Directed Graph

Hard
62.6%
Updated 6/1/2025

Largest Color Value in a Directed Graph

1. What is this problem about?

The Largest Color Value in a Directed Graph is a complex graph problem that combines traversal with optimization. You are given a directed graph where each node is assigned a color (represented by a lowercase letter). A path's "color value" is defined as the maximum number of nodes of the same color along that path. Your task is to find the maximum color value among all possible paths in the graph. If the graph contains a cycle, the color value is considered infinite (or technically, you return -1 because a path can go around a cycle forever).

2. Why is this asked in interviews?

This Hash Table, Graph, Topological Sort coding problem is a frequent "Hard" level question at Google and Meta. It tests whether a candidate can detect cycles in a directed graph and perform efficient dynamic programming on a Directed Acyclic Graph (DAG). It requires managing a 2D state (nodes ×\times colors) and understanding that a path's color frequency can be computed by building upon the frequencies of its predecessors.

3. Algorithmic pattern used

The solution uses Kahn’s Algorithm for Topological Sort combined with Dynamic Programming.

  1. Cycle Detection: If we cannot process all nodes using topological sort, the graph has a cycle, and we return -1.
  2. DP State: Let dp[u][char] be the maximum count of char on any path ending at node u.
  3. Transition: When processing an edge (u, v), for every color c, we update: dp[v][c] = max(dp[v][c], dp[u][c]).
  4. Final Step: After updating dp[v] with all its predecessors, we increment dp[v][color_of_v]. The overall answer is the maximum value in the entire dp table.

4. Example explanation

Suppose we have A -> B -> C and colors are A: 'r', B: 'g', C: 'r'.

  • Start with node A: dp[A]['r'] = 1, all others 0.
  • Process edge A -> B: dp[B]['r'] = max(0, 1) = 1. Then increment dp[B]['g'] to 1.
  • Process edge B -> C: dp[C]['r'] = max(0, 1) = 1, dp[C]['g'] = max(0, 1) = 1. Then increment dp[C]['r'] to 2. The maximum color value is 2 (for color 'r').

5. Common mistakes candidates make

  • Ignoring cycles: Failing to check if the graph is a DAG before proceeding with DP.
  • Space Complexity: Creating a 2D DP array of size N×26N \times 26 is fine, but some try to use more complex hash maps that lead to overhead.
  • Incorrect update order: Trying to use DFS without memoization or failing to update colors correctly during the topological sort.
  • Wait-and-see: Not realizing that each node only needs to update its successors after all its own predecessors have been processed (the essence of topological sort).

6. Interview preparation tip

Whenever you see "maximum value along a path" in a directed graph, think: "Is this a DAG?" if yes, Topological Sort + DP is your go-to strategy. If cycles are possible, cycle detection (using in-degrees or DFS coloring) is the first mandatory step.

Similar Questions