
You are given two independent coding tasks similar to what a DevOps engineer might face when reasoning about Meta infrastructure systems such as Terragraph routing and large-scale log compaction. Implement both algorithms within one interview.
Given a 2D grid of 0s and 1s, where 0 means open and 1 means blocked, return the length of the shortest path from the top-left cell to the bottom-right cell moving in 4 directions. Return -1 if no path exists.
Given an array files where files[i] is the size of the ith file, merge adjacent files until one file remains. The cost of merging two groups is the sum of their sizes. Return the minimum total cost.
shortest_path_grid(grid) takes List[List[int]] and returns an integer.min_merge_cost(files) takes List[int] and returns an integer.Example 1:
grid = [[0,0,0],[1,1,0],[0,0,0]]4Example 2:
files = [10, 20, 30]9010+20=30, then 30+30=60, total cost 90.1 <= rows, cols <= 200grid[i][j] is 0 or 11 <= files.length <= 3001 <= files[i] <= 10^4grid = [[0,0,0],[1,1,0],[0,0,0]], files = [10,20,30]Output{"shortest_path": 4, "min_merge_cost": 90}WhyThe grid path reaches the bottom-right in 4 moves. For files, merge 10 and 20 first, then merge the result with 30 for total cost 90.grid = [[0,1],[1,0]], files = [5,5,5,5]Output{"shortest_path": -1, "min_merge_cost": 40}WhyThe destination is unreachable in the grid. For files, optimal adjacent merges produce a total minimum cost of 40.1 <= rows, cols <= 200grid[i][j] is either 0 or 11 <= files.length <= 3001 <= files[i] <= 10^4def solve_dual_problems(grid, files):