Easy · Graphs

Find Degree of Vertex

Given an undirected graph as adjacency lists adj (each edge appears in both endpoints' rows) and a vertex v, return the degree of v, the number of edges touching it. For a directed graph given as in- and out-adjacency lists, return in-degree + out-degree.

Examples

Example 1

{
  "adj": [
    [1, 2, 3],
    [0, 2],
    [0, 1, 3, 4],
    [0, 2],
    [2, 5],
    [4]
  ],
  "query": 2
}

Output: deg(2) = 4

Example 2

{
  "adj": [
    [1],
    [0, 2, 3],
    [1, 3],
    [1, 2, 4],
    [3]
  ],
  "query": 3
}

Output: deg(3) = 3

Rebuild it in the studio

Read every interview problem free. Ten rooms need no account. A token opens a problem in full — Pro never counts.

More Graphs problems