Data Structures & AlgorithmsModerateTCS NQT Digital · Accenture
Problem: Vertices 0–5 form a graph with edges 0-1, 1-2, 2-3, 3-0 and 4-5. A BFS from vertex 0 gives each vertex a distance. What is the sum of these distances over the vertices that are reachable from 0?
- A. 4
- B. 5
- C. 3
- D. 6
Correct answer: A. 4
BFS distances are 0 for the source, 1 for vertices 1 and 3, and 2 for vertex 2, totalling 0+1+1+2 = 4. Vertices 4 and 5 are unreachable and excluded.
More Coding Interview Problems questions
- Problem: A stack-sortable check is applied to the permutation [3, 1, 2]: elements are pushed in order and popped whenever the top equals…
- Problem: The array [5, 2, 8, 2, 9, 1, 5, 5] contains repeated values. How many distinct values appear exactly twice?
- Problem: What is the maximum sum of a subsequence of [4, −1, 2, 1] with no two adjacent elements chosen (the empty selection is not…
- Problem: The string "ABCABCABC" is read and each letter is replaced by its position in the alphabet (A=1, B=2, C=3). What is the sum of…
- Problem: From the list [12, 18, 24, 30, 36], what is the number of distinct prime divisors of the product of all five numbers?
- Problem: The array [7, 1, 5, 3, 6, 4] is processed to find the maximum profit from one buy and one later sell (0 if none). What is the…