One common graph storage format is called an adjacency list.
In this format, every node has an array of connected neighbors: an adjacency list. We could store these arrays in a hash map where the keys represent the node and the values are the adjacency lists.
For instance, here's one graph:
And, here's how we'd represent it using a hash map:
Since node 3 has edges to nodes 1 and 2, graph.get(3) has the adjacency list {1, 2}.
Adjacency List interview questions to practice
Each one walks you through a real coding interview problem one hint at a time.
- MeshMessage » You wrote a trendy new messaging app, MeshMessage, to get around flaky cell phone coverage. But messages are taking too long to send...
- Graph Coloring » Color the nodes in a graph so adjacent nodes always have different colors.
Your first 3 questions are free. Just sign in, no credit card.
See also:
Interview coming up?
Get the free 7-day email crash course. You'll learn how to think algorithmically, so you can break down tricky coding interview questions.
No prior computer science training necessary—we'll get you up to speed quickly, skipping all the overly academic stuff.
No spam. One-click unsubscribe whenever.
You're in! Head over to your email inbox right now to read day one!
Interview Cake