Data Structures & AlgorithmsVery hardICPC regional · Product Company SDE
Problem: A 5×5 grid has 1s and 0s: 11000 / 11011 / 00100 / 01100 / 10001 (rows top to bottom). You may flip exactly one 0 into a 1. What is the largest possible 4-directionally connected component of 1s afterwards?
- A. 4
- B. 10
- C. 12
- D. 11
Correct answer: B. 10
Label components first (sizes 4, 3, 2, 1, 1), then for each 0 add 1 plus the DISTINCT neighbouring components; the best flip gives 10. The largest component before any flip is 4, allowing diagonal connections gives 12, and adding neighbour sizes without de-duplicating a component touched from two sides gives 11.
More Coding Interview Problems questions
- Problem: A DAG with vertices 0–5 has weighted edges 0→1 (2), 0→2 (1), 1→3 (2), 2→3 (3), 2→4 (1), 3→5 (1), 4→5 (5). How many distinct…
- Problem: An undirected connected graph has 9 vertices and 13 edges. What is the cyclomatic number (the number of independent cycles…
- Problem: An undirected graph on 7 vertices (0–6) has degrees 0:3, 1:2, 2:4, 3:1, 4:2, 5:3, 6:1. How many vertices have odd degree? (Such a…
- Problem: Vertices 0–5 with undirected edges 0-1, 1-2, 2-0, 2-3, 3-4, 4-5, 5-3. How many articulation points (cut vertices, whose removal…
- Problem: Using a min-heap, the stream 9, 4, 7, 1, 8, 2, 6, 3, 5 is processed as follows: after each insertion, pop the minimum if the heap…
- Problem: For the array [4, 1, 5, 2, 6, 3, 7], compute, for each index i, the distance to the next strictly greater element to its right (0…