CISCO OA QNA FY'26 Maximum Strength Path – Custom Graph/Grid Problem

Problem Statement

You are given an n x m grid. Each cell may either be blocked or free. You are also given a list of blocked cell positions.

You start at cell (1, 1) and want to reach cell (n, m) by moving up, down, left, or right to adjacent unblocked cells.

A path's strength is defined as the minimum Manhattan distance from any visited cell to the nearest blocked cell.

Among all valid paths from (1, 1) to (n, m), return the one with the maximum strength.
If multiple such paths exist, choose the one that visits the fewest number of cells.

Return an array of two integers:

  • The maximum strength achievable.

  • The minimum number of cells visited to achieve that strength.

If it is not possible to reach (n, m), return [-1, -1].

Example 1:

Input:
n = 4
m = 3
blockedPositions = [[1, 3], [2, 3]]

Output:
[2, 6]

Explanation:
One optimal path is: (1,1) → (2,1) → (3,1) → (4,1) → (4,2) → (4,3)
The minimum distance from any visited cell to a blocked cell is 2 (i.e., strength = 2)
Number of visited cells = 6

Example 2:

Input:
n = 3
m = 3
blockedPositions = [[1,2], [2,2], [3,2]]

Output:
[-1, -1]

Explanation:
There is no valid path from (1,1) to (3,3).

Constraints:

  • 1 <= n, m <= 1000

  • 0 <= blockedPositions.length <= n * m

  • 1 <= blockedPositions[i][0] <= n

  • 1 <= blockedPositions[i][1] <= m

  • The cell (1,1) and (n,m) are guaranteed to be initially unblocked.

Function Signature (Python):

def findOptimalPair(n: int, m: int, blockedPositions: List[List[int]]) -> List[int]:

Comments (0)