You only have free questions left (including this one).

But it doesn't have to end here! Sign up for the 7-day coding interview crash course and you'll get a free Interview Cake problem every week.

Given an undirected graph with maximum degree D, find a graph coloring using at most D+1 colors.

For example:

First described by Robert Frucht in 1939, the Frucht graph is a 3-regular graph with 12 vertices, 18 edges, and no nontrivial symmetries.

This graph's maximum degree (D) is 3, so we have 4 colors (D+1). Here's one possible coloring:

The Frucht graph with legal coloring.

Graphs are represented by an array of N node objects, each with a label, a hash set of neighbors, and a color:

import java.util.Collections; import java.util.HashSet; import java.util.Optional; import java.util.Set; public class GraphNode { private String label; private Set<GraphNode> neighbors; private Optional<String> color; public GraphNode(String label) { this.label = label; neighbors = new HashSet<GraphNode>(); color = Optional.empty(); } public String getLabel() { return label; } public Set<GraphNode> getNeighbors() { return Collections.unmodifiableSet(neighbors); } public void addNeighbor(GraphNode neighbor) { neighbors.add(neighbor); } public boolean hasColor() { return color.isPresent(); } public String getColor() { return color.get(); } public void setColor(String color) { this.color = Optional.ofNullable(color); } } GraphNode a = new GraphNode("a"); GraphNode b = new GraphNode("b"); GraphNode c = new GraphNode("c"); a.addNeighbor(b); b.addNeighbor(a); b.addNeighbor(c); c.addNeighbor(b); GraphNode[] graph = new GraphNode[] { a, b, c };

D+1 colors is always enough. Does your method ever need more colors than that?

Does your method go through every color for every node? You can do better. You don't want N*D in your final runtime.

We can color a graph in linear time and space (on the number of nodes, edges and/or the maximum degree).

What if the input graph has a loop? Does your method handle that reasonably?

Start your free trial!

Log in or sign up with one click to get immediate access to free mock interview questions

Where do I enter my password?

Actually, we don't support password-based login. Never have. Just the OAuth methods above. Why?

  1. It's easy and quick. No "reset password" flow. No password to forget.
  2. It lets us avoid storing passwords that hackers could access and use to try to log into our users' email or bank accounts.
  3. It makes it harder for one person to share a paid Interview Cake account with multiple people.

Start your free trial!

Log in or sign up with one click to get immediate access to free mock interview questions

Where do I enter my password?

Actually, we don't support password-based login. Never have. Just the OAuth methods above. Why?

  1. It's easy and quick. No "reset password" flow. No password to forget.
  2. It lets us avoid storing passwords that hackers could access and use to try to log into our users' email or bank accounts.
  3. It makes it harder for one person to share a paid Interview Cake account with multiple people.

time where N is the number of nodes and M is the number of edges.

The runtime might not look linear because we have outer and inner loops. The trick is to look at each step and think of things in terms of the total number of edges (M) wherever we can:

  • We check if each node appears in its own hash set of neighbors. Checking if something is in a hash set is , so doing it for all N nodes is .
  • When we get the illegal colors for each node, we iterate through that node's neighbors. So in total, we cross each of the graphs M edges twice: once for the node on either end of each edge. time.
  • When we assign a color to each node, we're careful to stop checking colors as soon as we find one that works. In the worst case, we'll have to check one more color than the total number of neighbors. Again, each edge in the graph adds two neighbors—one for the node on either end—so there are 2*M neighbors. So, in total, we'll have to try colors.

Putting all the steps together, our complexity is .

What about space complexity? The only thing we're storing is the illegalColors hash set. In the worst case, all the neighbors of a node with the maximum degree (D) have different colors, so our hash set takes up space.

  1. Our solution runs in time but takes space. Can we get down to space?
  2. Our solution finds a legal coloring, but there are usually many legal colorings. What if we wanted to optimize a coloring to use as few colors as possible?

The lowest number of colors we can use to legally color a graph is called the chromatic number.

There's no known polynomial time solution for finding a graph’s chromatic number. It might be impossible, or maybe we just haven’t figured out a solution yet.

We can't even determine in polynomial time if a graph can be colored using a given k colors. Even if k is as low as 3.

We care about polynomial time solutions (n raised to a constant power, like ) because for large ns, polynomial time algorithms are more practical to actually use than higher runtimes like exponential time (a constant raised to the power of n, like ). Computer scientists usually call algorithms with polynomial time solutions feasible, and problems with worse runtimes intractable.

The problem of determining if a graph can be colored with k colors is in the class of problems called NP (nondeterministic polynomial time). This means that in polynomial time, we can verify a solution is correct but we can’t come up with a solution. In this case, if we have a graph that's already colored with k colors we verify the coloring uses k colors and is legal, but we can't take a graph and a number k and determine if the graph can be colored with k colors.

If you can find a solution or prove a solution doesn't exist, you'll win a $1,000,000 Millennium Problem Prize.

For coloring a graph using as few colors as possible, we don’t have a feasible solution. For real-world problems, we'd often need to check so many possibilities that we’ll never be able to use brute-force no matter how advanced our computers become.

One way to reliably reduce the number of colors we use is to use the greedy algorithm but carefully order the nodes. For example, we can prioritize nodes based on their degree, the number of colored neighbors they have, or the number of uniquely colored neighbors they have.

Start your free trial!

Log in or sign up with one click to get immediate access to free mock interview questions

Where do I enter my password?

Actually, we don't support password-based login. Never have. Just the OAuth methods above. Why?

  1. It's easy and quick. No "reset password" flow. No password to forget.
  2. It lets us avoid storing passwords that hackers could access and use to try to log into our users' email or bank accounts.
  3. It makes it harder for one person to share a paid Interview Cake account with multiple people.

Reset editor

Powered by qualified.io

. . .