0Pricing
Coding Interview Prep · Lesson

Adjacency Lists from Input

Build the graph that contests give you.

Adjacency Lists from Input is a free Coding Interview Prep lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Coding Interview Prep learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

What a Graph Really Is

A graph is just dots called nodes joined by lines called edges. Cities linked by roads is a graph you already know. 🗺️

Nodes and Edges

Each node is a thing, and each edge says two nodes are connected. Contest graphs usually number nodes from 1 to n.

The Adjacency List

The go-to storage for contests is an adjacency list: for every node, keep a list of its direct neighbors.

adj = [[] for _ in range(n + 1)]

Why Not a Matrix

A matrix uses n squared memory, which explodes for large n. An adjacency list stores only edges that exist, so it scales.

Reading the First Line

Most inputs start with two numbers: n nodes and m edges. Read them first so you know how many edges to expect.

n, m = map(int, input().split())

One Edge Per Line

Each of the next m lines gives a pair u v. That single edge means u and v are directly connected.

u, v = map(int, input().split())

Undirected Means Both Ways

For an undirected edge, add the link in both directions. You can walk from u to v and from v to u.

adj[u].append(v)
adj[v].append(u)

Directed Means One Way

For a directed edge, store only u to v. Read the statement carefully to know which kind you have.

adj[u].append(v)

Building It in a Loop

Loop m times, read each pair, and fill the lists. After the loop your adjacency list holds the whole graph.

for _ in range(m):
    u, v = map(int, input().split())
    adj[u].append(v)
    adj[v].append(u)

1-Indexed vs 0-Indexed

If nodes start at 1, size your list as n plus 1 so index n is valid. Mixing up indexing causes silent bugs.

Visit a Node's Neighbors

Once built, exploring is easy: loop over adj of a node to reach every neighbor in one step.

for nb in adj[u]:
    print(nb)

Quick Check

You read an undirected edge u v. What do you store?

Recap

You now build a graph as an adjacency list: read n and m, loop the edges, and add both directions when undirected. 🎉

Frequently asked questions

Is the “Adjacency Lists from Input” lesson free?

Yes — the full text of “Adjacency Lists from Input” is free to read here on the web, and the Coding Interview Prep course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Coding Interview Prep course, upgrade to CoddyKit PRO.

What will I learn in “Adjacency Lists from Input”?

Build the graph that contests give you. You practise Coding Interview Prep with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Coding Interview Prep?

No prior experience is required. Coding Interview Prep on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Adjacency Lists from Input” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Coding Interview Prep lesson?

Yes. Every Coding Interview Prep lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Adjacency Lists from Input
  2. BFS for Shortest Unweighted Paths
  3. DFS, Recursion & Iterative Stacks
  4. Connected Components & Flood Fill
← Back to Coding Interview Prep