Data Structures & AlgorithmsModerateTCS NQT · Accenture
Problem: A simple graph has 6 vertices labelled 1–6. Vertex i is adjacent to j exactly when gcd(i, j) = 1 and i ≠ j. How many edges does this graph have?
- A. 12
- B. 22
- C. 4
- D. 11
Correct answer: D. 11
Of the C(6,2) = 15 pairs, the 4 sharing a factor are (2,4), (2,6), (3,6), (4,6), leaving 11 edges. Counting gcd(1, 1) = 1 as a self-loop gives 12, counting ordered pairs gives 22, and counting the non-coprime pairs gives 4.
More Coding Interview Problems questions
- Problem: What is the number of inversions in the array [6, 2, 8, 1, 9, 4, 3, 7, 5]? (An inversion is a pair of indices i < j with a[i] >…
- Problem: Using a single counter, a sequence of commands is applied to the value x = 0: "+5", "×2", "−3", "×2", "+1", "÷4" (integer…
- Problem: In a binary max-heap stored in an array with 1-based indexing, the array is [40, 30, 35, 10, 20, 25, 28, 5, 8, 12]. An operation…
- Problem: In the array [3, 8, 1, 9, 4, 7, 2, 6, 5], what is the length of the longest subarray whose sum is exactly 20?
- Problem: A sorted list 2, 4, 6, 8, 10, 12, 14 is treated as a rotated array: the first 3 values are moved to the end, giving 8, 10, 12…
- Problem: Four processes arrive at times 0, 2, 4, 6 with burst times 6, 2, 8, 3. The scheduler is shortest-remaining-time-first…