Data Structures & AlgorithmsVery hardProduct company OA
In this grid, how many islands of 1s are there with 4-directional connectivity, and how many with 8-directional (diagonals also connect)?
In this grid, how many islands of 1s are there with 4-directional connectivity, and how many with 8-directional (diagonals also connect)?
1 0 0 1 1 0 1 0 0 1 0 0 1 0 0 1 0 0 0 1 1 1 0 1 0
- A. 7 and 3
- B. 6 and 4
- C. 8 and 4
- D. 7 and 4
Correct answer: D. 7 and 4
4-connected: {(0,0)}, {(1,1)}, {(2,2)}, {(0,3),(0,4),(1,4)}, {(3,0),(4,0),(4,1)}, {(3,4)}, {(4,3)} = 7. With diagonals, (0,0)-(1,1)-(2,2) join into one island and (3,4)-(4,3) join into another; the top-right group and the bottom-left group touch nothing diagonally, so they stay separate: 4. Merging (2,2) with a neighbouring group (it has none diagonally) gives 3.
Practise more Trees, Graphs, Recursion and Dynamic Programming questionsTrees, Graphs, Recursion and Dynamic Programming: notes and key points
More Trees, Graphs, Recursion and Dynamic Programming questions
- What is the maximum path sum in this binary tree, where a path joins any two nodes along parent–child links and need not pass through the…
- A binary trie over 4-bit numbers finds the maximum XOR of any pair from [3, 10, 5, 9, 2, 8]. What is the maximum XOR?
- In a binary tree whose root is at level 0, what is the maximum number of nodes at level 5?
- A simple directed graph (no self-loops, at most one edge from u to v) has 6 vertices. What is the maximum number of edges?
- Which algorithm finds shortest paths (by number of edges) from one source in an UNWEIGHTED graph in O(V + E) time?
- A binary tree is stored in level order in a 0-indexed array (children of i at 2i + 1 and 2i + 2; None = no node): [1, 2, 3, None, 4, 5]…