Data Structures & AlgorithmsHardProduct Company SDE · Amazon OA
Problem: Vertices 0–3 with directed edges 0→1, 0→2, 1→2, 2→0, 1→3, 2→3, 3→3. How many directed walks of exactly length 3 start at vertex 0 and end at vertex 3? (A walk may repeat vertices and edges, and the self-loop 3→3 may be used.)
- A. 3
- B. 4
- C. 2
- D. 5
Correct answer: A. 3
Walk counts of length k are entries of the k-th power of the adjacency matrix; propagating counts step by step does the same. Counting simple paths only would exclude the walks that revisit vertices.
More Coding Interview Problems questions
- Problem: A rooted tree has root 1 and children: 1→2 (weight 4), 1→3 (weight 1), 2→4 (weight 2), 2→5 (weight 7), 3→6 (weight 3). The…
- Problem: A rooted binary tree stored as a parent array over nodes 0–9: parent of node i is [-1, 0, 0, 1, 1, 2, 3, 3, 4, 5][i] (node 0 is…
- Problem: A rooted tree of 8 nodes (root 1) is given by children lists: 1: [2, 3], 2: [4, 5], 3: [6], 4: [], 5: [7, 8], 6: [], 7: [], 8…
- Problem: The house-robber rule applies on a tree: you pick a set of nodes with no parent–child pair both chosen, maximising the total…
- Problem: The Z-function of a string s is defined as z[i] = the length of the longest common prefix of s and s[i:]; z[0] is conventionally…
- Problem: Count the distinct non-empty substrings of "abcabc" (two substrings are the same if their letters are identical, regardless of…