# Algorithms Challenge - #4: Graph Walk Challenge

**URL:** <https://forum.kirupa.com/t/algorithms-challenge-4-graph-walk-challenge/684117>\
**Category:** programming\
**Created:** [November 21, 2026, 4:05pm UTC](https://forum.kirupa.com/t/algorithms-challenge-4-graph-walk-challenge/684117 "2026-11-21T16:05:07Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Yoshiii](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/yoshiii/32/31156_2.png) [@Yoshiii](https://forum.kirupa.com/u/Yoshiii)\
**Post date:** [November 21, 2026, 4:05pm UTC](https://forum.kirupa.com/t/algorithms-challenge-4-graph-walk-challenge/684117/1 "2026-11-21T16:05:07Z")

</div>

Implement a function that finds the length of the longest simple path (no repeated nodes) in an undirected small graph given as adjacency lists. Return the length in number of edges for the longest path; if the graph has no edges, return 0. The function should be efficient for up to about 15 nodes and handle disconnected graphs by evaluating each component.

**Examples:**

- `longest_path_length(adj)` returns `4` for a path of 5 nodes connected in sequence
- `longest_path_length({1:[2,3],2:[1,3],3:[1,2]})` returns `2`
- Edge case: a graph with a single node and no edges returns `0`

```python
def longest_path_length(adj):
    """Compute the length of the longest simple path in an undirected graph.

    Parameters:
        adj: dict[int, list[int]] - adjacency list of the graph

    Returns:
        int - number of edges in the longest simple path
    """
    pass

```

**Rules:**

- Any language or pseudocode is fine. The stub just shows the shape.
- Say the time complexity of your approach.
- one well-defined function with a clear input and output
- 2 or 3 sample calls with expected results, including an edge case
- size suitable for an interview warm-up

Post your solution as a reply. Answer goes up in about a day.
