Answer common graph, grid, and array tasks
Company: Microsoft
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: nan
Interview Round: Technical Screen
Quick Answer: This set of problems evaluates algorithmic skills across arrays, graph theory, and grid/matrix traversal—covering sliding-window and contiguous-segment reasoning, dependency resolution and topological ordering with cycle detection, connected-component analysis and attribute aggregation in 2D grids, and influencer identification in adjacency matrices. They are commonly asked in the Coding & Algorithms domain to measure understanding of fundamental data structures and graph concepts, reasoning about connectivity and ordering under constraints, and the ability to design efficient, practical solutions that require both conceptual understanding and practical application.
Part 1: Largest continuous island after filling sea (1D)
Constraints
- 1 <= len(islands) <= 2e5
- 0 <= material <= len(islands)
- Each islands[i] is either 0 or 1
Examples
Input: ([0, 1, 0, 1, 1, 1], 1)
Expected Output: 5
Explanation: Flip the zero at index 2 to get a longest contiguous segment of length 5.
Input: ([1, 1, 1, 1], 2)
Expected Output: 4
Explanation: The array is already all land, so the answer is the full length.
Input: ([0, 0, 0], 2)
Expected Output: 2
Explanation: At most two ocean cells can be flipped, so the best contiguous land segment has length 2.
Input: ([0], 0)
Expected Output: 0
Explanation: Single-cell edge case with no material.
Hints
- Think about maintaining a window that contains at most material zeros.
- If the window becomes invalid, move its left side forward until the number of zeros is small enough again.
Part 2: Resolve software package dependencies
Constraints
- 0 <= len(packages) <= 2e5
- The total number of required packages plus dependency edges is assumed to be manageable in memory
- Package names are strings
- Dependencies may mention packages not present as keys
Examples
Input: (['app'], {'app': ['lib', 'core'], 'lib': ['util'], 'core': []})
Expected Output: ['util', 'lib', 'core', 'app']
Explanation: util is implied even though it is not a key. Each dependency appears before the package that needs it.
Input: (['a', 'b'], {'a': ['c'], 'b': ['c']})
Expected Output: ['c', 'a', 'b']
Explanation: Shared dependency c appears only once, before both requested packages.
Input: (['pkg'], {})
Expected Output: ['pkg']
Explanation: A package with no dependencies is installed by itself.
Input: ([], {'a': ['b']})
Expected Output: []
Explanation: No requested packages means nothing needs to be installed.
Input: (['a'], {'a': ['b'], 'b': ['a']})
Expected Output: 'ValueError'
Explanation: The required dependency graph contains a cycle, so the function should raise ValueError.
Hints
- This is a topological ordering problem on the required subgraph.
- Use three states for DFS nodes: unseen, visiting, and done. Encountering a visiting node means there is a cycle.
Part 3: Count the number of clouds in a heat-index grid
Constraints
- 0 <= number of rows, columns <= 500
- 0 <= sky[r][c] <= 9
- Connectivity is 4-directional
Examples
Input: [[5, 2, 2], [6, 5, 2], [1, 7, 8]]
Expected Output: 2
Explanation: There is one cloud of size 3 in the top-right area and one single-cell cloud at the bottom-left.
Input: [[9, 9], [9, 9]]
Expected Output: 0
Explanation: No cell has value at most 4.
Input: [[0]]
Expected Output: 1
Explanation: A single cloud cell forms one cloud.
Input: []
Expected Output: 0
Explanation: Empty grid edge case.
Hints
- Whenever you find an unvisited cloud cell, start a flood fill from it.
- Mark all cells in that component so they are not counted again.
Part 4: Find the maximum cloud size
Constraints
- 0 <= number of rows, columns <= 500
- 0 <= sky[r][c] <= 9
- Connectivity is 4-directional
Examples
Input: [[5, 2, 2], [6, 5, 2], [1, 7, 8]]
Expected Output: 3
Explanation: The largest cloud contains the three connected cells in the top-right area.
Input: [[0, 0], [0, 0]]
Expected Output: 4
Explanation: All four cells are cloud and all are connected.
Input: [[9, 9], [9, 9]]
Expected Output: 0
Explanation: There are no clouds.
Input: []
Expected Output: 0
Explanation: Empty grid edge case.
Hints
- Use the same flood-fill idea as cloud counting, but track the number of cells visited in each component.
- Update the best answer after finishing each component.
Part 5: Identify and count thunderstorm clouds
Constraints
- 0 <= number of rows, columns <= 500
- 0 <= sky[r][c] <= 9
- A thunderstorm cloud satisfies thunder_cells * 2 >= cloud_size
Examples
Input: [[1, 4, 5], [0, 3, 9], [6, 9, 1]]
Expected Output: 2
Explanation: There are two clouds. The 2x2 cloud has 2 of 4 cells at most 1, and the single-cell cloud also qualifies.
Input: [[2, 4], [4, 2]]
Expected Output: 0
Explanation: All four cells form one cloud, but none are at most 1.
Input: [[1, 9, 1]]
Expected Output: 2
Explanation: Two isolated one-cell clouds both qualify as thunderstorm clouds.
Input: []
Expected Output: 0
Explanation: Empty grid edge case.
Hints
- During each flood fill, track both the total component size and how many cells have value at most 1.
- To avoid floating-point math, compare 2 * thunder_cells with cloud_size.
Part 6: Find an influencer (celebrity) in a following matrix
Constraints
- 0 <= N <= 5000
- followingMatrix is an N x N boolean matrix
- followingMatrix[i][i] == False for all i
Examples
Input: [[False, True, True], [False, False, True], [False, False, False]]
Expected Output: 2
Explanation: User 2 follows nobody, and users 0 and 1 both follow user 2.
Input: [[False, True], [True, False]]
Expected Output: -1
Explanation: Both users follow someone, so there is no influencer.
Input: [[False]]
Expected Output: 0
Explanation: With one user, the conditions hold vacuously.
Input: []
Expected Output: -1
Explanation: Empty network edge case.
Hints
- If user a follows user b, then a cannot be the influencer.
- After narrowing down to one candidate, you still must verify both the candidate's row and column.
Part 7: Find the user with the maximum number of followers
Constraints
- 0 <= N <= 5000
- followingMatrix is an N x N boolean matrix
- followingMatrix[i][i] == False for all i
Examples
Input: [[False, True, True], [False, False, True], [True, False, False]]
Expected Output: 2
Explanation: User 2 is followed by users 0 and 1, which is the highest count.
Input: [[False, True], [True, False]]
Expected Output: 0
Explanation: Both users have one follower, so the smaller ID is returned.
Input: [[False, False, False], [False, False, False], [False, False, False]]
Expected Output: 0
Explanation: Everyone has zero followers, so tie-breaking returns user 0.
Input: []
Expected Output: -1
Explanation: Empty network edge case.
Hints
- The answer depends on column counts, not row counts.
- A simple way is to count how many True values appear in each column, excluding the diagonal.