Data Structures & AlgorithmsModerateTCS NQT Digital · Infosys
Problem: A tree on 6 nodes has edges 1-2, 1-3, 1-4, 4-5, 4-6. How many pairs of edges share a common endpoint (that is, unordered pairs of distinct edges that touch at a vertex)?
- A. 10
- B. 12
- C. 6
- D. 15
Correct answer: C. 6
Nodes 1 and 4 have degree 3 and all others degree 1, so Σ C(d, 2) = 3 + 3 = 6. 10 counts all pairs of the 5 edges, d(d − 1) counts ordered pairs for 12, and 15 counts vertex pairs.
More Coding Interview Problems questions
- Problem: The string "cbaebabacd" is scanned with windows of length 3. How many windows are anagrams of "abc" (same letters, same counts…
- Problem: The string "aaaaa" is split into non-overlapping occurrences of "aa" from left to right (greedily taking each match). How many…
- Problem: In the string "aabcbcdbca", what is the length of the shortest substring that contains exactly 3 distinct letters?
- Problem: What is the length of the longest palindromic substring of "abcbxa" that can be made a palindrome after deleting at most one…
- Problem: How many sequences made of the numbers 1 and 2 sum to 8, where no two 2s are adjacent (order matters)?
- Problem: From the array [−10, −10, 5, 2], pick three numbers (distinct positions) to maximise their product. What is the maximum product?