Meta VO Interview Coding Question: Continuous Subarray Sum / Shortest Path in a Binary Grid

14 Views
No Comments

Given a sequence of integers and an integer total target, return whether a continuous sequence of integers sums up to target.

Example:

[1, 3, 1, 4, 23], 8: True (because 3 + 1 + 4 = 8)
[1, 3, 1, 4, 23], 7: False

You are given a game board represented as a 2D array of zeroes and ones. Zero stands for passable positions and one stands for impassable positions. Design an algorithm to find the shortest path from top left corner to bottom right corner.

For example, for the following board:

entrance -> 0 0 0 0 0 0 0
0 0 1 0 0 1 0
0 0 1 0 1 1 0
0 0 1 0 1 0 1
1 1 1 0 0 0 0 -> exit

A possible path is:

entrance -> + + + + 0 0 0
0 0 1 + 0 1 0
0 0 1 + 1 1 0
0 0 1 + 1 0 1
1 1 1 + + + + -> exit

Assuming a zero-indexed grid of rows and columns, with (0, 0) at the top left corner (entrance), we’d return the path coordinates from entrance to exit.

This prompt combines two classic interview patterns: checking whether any contiguous subarray sums to a target, and finding the shortest path in a binary grid from the top-left to the bottom-right corner. The array problem is typically solved with prefix sums and a hash set in linear time, while the grid problem is best handled with BFS to guarantee the shortest path and reconstruct the route with parent pointers.

END
 0