Data Structures & AlgorithmsModerateTCS NQT · Infosys
Problem: The cycle graph C8 has vertices 0–7 with i adjacent to (i+1) mod 8. How many unordered pairs of vertices are at distance exactly 3 along the shorter way around the cycle?
- A. 16
- B. 8
- C. 4
- D. 28
Correct answer: B. 8
Each vertex i pairs with i + 3 (and i − 3), and the shorter way is 3 because 3 < 8 − 3, so there are 8 × 2 / 2 = 8 unordered pairs. Counting ordered pairs gives 16, counting distance 4 (opposite vertices) gives 4, and 28 counts every pair.
More Coding Interview Problems questions
- Problem: A height-balanced binary search tree is built over the sorted keys 1, 2, …, 10 by always taking the middle element as the root…
- Problem: Given the parent array p = [−1, 0, 0, 1, 1, 2, 4, 4] for nodes 0–7 (node 0 is the root), how many nodes have exactly one child?
- Problem: A binary tree in level order (None for absent nodes) is [5, 4, 8, 11, None, 13, 4, 7, 2, None, None, 5, 1]. How many root-to-leaf…
- Problem: Count the odd-length palindromic substrings of "racecarxyz" that are centred at index 3 (the 'e' of "racecar"), where the centre…
- Problem: What is the lexicographically next permutation of the string "bcdea" (the next arrangement in dictionary order using the letters…
- Problem: How many substrings of "abcabc" contain all three letters a, b and c at least once?