Data Structures & AlgorithmsVery hardGATE CS · TCS NQT
In the undirected graph with edges (1,2), (1,3), (2,4), (3,4), (4,5), (3,6), how many vertices are at shortest-path distance exactly 2 from vertex 1?
- A. 3
- B. 2
- C. 4
- D. 5
Correct answer: B. 2
A BFS from vertex 1 assigns distance 1 to 2 and 3, then distance 2 to the unvisited neighbours 4 and 6. Vertex 5 is at distance 3.
Practise more Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questionsArrays, Strings, Linked Lists, Stacks, Queues and Hashing: notes and key points
More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions
- What is the minimum number of knight moves needed to go from square (0, 0) to (3, 3) on an 8 by 8 board (coordinates 0 to 7)?
- Two sorted linked lists are [1, 5, 9, 12] and [2, 3, 8]. They are merged into one sorted list. What is the 5th value of the merged list…
- In the array [2, 8, 5, 5, 3, 7, 5], how many index pairs i < j have a[i] + a[j] = 10?
- How many distinct sums are obtained by contiguous non-empty subarrays of [1, -1, 2, -2]?
- What is the length of the longest substring of aabbcaabcc in which every character occurs at least twice?
- How many palindromic substrings of length at least 2 does abcbaccab contain (counted by position)?