Code Room
CodingHardcod-g648
Subject MatrixLevel Senior–Staff~35 minCommon in Algorithms & data structures interviewsIndustries Software development, Technology

Question

You are given an n x n adjacency matrix `adj` of a directed graph (adj[i][j] == 1 means an edge i->j, else 0), a start node `src`, a destination node `dst`, and an integer `k`. Return the number of distinct walks of EXACTLY k edges from `src` to `dst`, modulo 1_000_000_007. n is up to 50 and k can be as large as 10^9, so you must use fast matrix exponentiation rather than DP over k.

Implement
count_walks(adj: list[list[int]], src: int, dst: int, k: int) → int
Examples
in[[[0,1,0],[0,0,1],[1,0,0]],0,0,3]out1
What a strong answer looks like

State your approach and its time/space complexity out loud before you optimize. Handle the edge cases (empty input, duplicates, overflow), and say why you chose this over the brute force. Green tests are the floor, not the grade.

Vibe coding: describe the solution in plain language (or narrate it) and the coach grades your approach. Generating runnable code from your description is coming next.

Run or narrate your approach, then ask the coach.