FastPrepShortest Grid Path with Weighted Obstacles
Problems/Shortest Grid Path with Weighted Obstacles

Shortest Grid Path with Weighted Obstacles

Apple logoApple● HardNEW GRADPHONE SCREEN

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported in an Apple new grad phone screen. The report asks for the shortest length, returning the path, and obstacles that can be crossed at a higher cost. It does not give the movement rules or tie rules. We chose those. Our version is about 85% the same as the reported question.

Given a 2D grid costs, find a minimum-cost path from start to target and return its cost, its number of moves and its cells. You may move up, down, left or right within the grid. Every cell can be entered. A high-cost cell is an obstacle that you can still cross at a higher price.

Entering cell (r,c) costs costs[r][c]. The starting cell is free. If several paths have the same total cost, return the lexicographically smallest list of coordinates. To compare two paths, find the first coordinate where they differ. The one with the smaller row wins. If the rows are equal, the one with the smaller column wins.

Return an integer matrix. The first row is [minimumCost,steps], where steps is the number of moves in the path. Each later row is one coordinate [row,column] of the path, from the start through the target. If start equals target, the first row is [0,0], followed by that one coordinate. Do not change the inputs.

Interview follow-ups

Start with a map where every cell costs 1 to enter. Then the cheapest path is the one with the fewest moves, and BFS with a deque finds it. Next, explain how to return the path itself. Finally, let obstacles cost more to cross, and use a priority queue to always expand the cell with the smallest known distance.

Function

shortestGridPath(costs: int[][], start: int[], target: int[]) → int[][]

Examples

Example 1

costs = [[1,1],[1,1]]start = [0,0]target = [1,1]return = [[2,2],[0,0],[0,1],[1,1]]

Both two-move paths cost 2 because the start is free. The path through (0,1) is lexicographically smaller than the one through (1,0), so it is the answer.

Example 2

costs = [[1,10,1],[1,1,1]]start = [0,0]target = [0,2]return = [[4,4],[0,0],[1,0],[1,1],[1,2],[0,2]]

The two-move route through the high-cost cell costs 11. The four-move route around it costs 4, so it is the answer.

Constraints

  • 1 <= costs.length <= 20
  • Each costs row has 1 to 20 entries.
  • 1 <= costs[i][j] <= 100
  • start and target each have exactly 2 entries.
  • 0 <= every coordinate <= 19
  • costs is rectangular, and start and target are valid [row,column] coordinates within it.

More Apple Phone Screen questions

  • Topological OrderingPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • LRU CachePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Merge IntervalsOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Subarray Product Less Than KPHONE SCREEN · Seen Nov 2026
  • Evaluate DivisionPHONE SCREEN · Seen Nov 2026
  • Reverse a Linked ListPHONE SCREEN · Seen Nov 2026
  • Validate a BST with a Custom ComparisonPHONE SCREEN · Seen Nov 2026
  • Design a Food Rating SystemPHONE SCREEN · Seen Oct 2026
  • Days Until Poisoned Plants StabilizePHONE SCREEN · Seen Oct 2026
  • Capture Surrounded Regions with Union-FindPHONE SCREEN · Seen Oct 2026

Similar questions at other companies

  • Find the Safest Path in a GridAmazon · PHONE SCREEN · ONSITE INTERVIEW
  • Shortest Distance from All BuildingsWaymo · PHONE SCREENWaymo interview questions
  • Shortest Path in a Grid with Obstacles EliminationMercor · PHONE SCREENMercor interview questions
  • Shortest Path in a Grid with Obstacles EliminationBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
  • Swim in Rising WaterGoogle · PHONE SCREENGoogle interview questions
  • Shortest Path in a Blocked GridSpotify · PHONE SCREEN
  • Shortest Path in a Binary MatrixMeta · PHONE SCREENMeta interview questions
  • Shortest Path with Directional Move CostsThe Walt Disney Company · PHONE SCREENThe Walt Disney Company interview questions
  • Shortest Path to Food with DirectionsZip · PHONE SCREENZip interview questions
  • Safest Shortest Path Through a GridSnowflake · OASnowflake interview questions

Apple interview experiences

  • Apple SWE Phone Screen: Subarray Products: 1 Experience
  • Apple New Grad SWE Java, Coding and Manager Rounds: 1 Experience
  • Apple AI Coding and Related Interviews: 17 Experiences
  • Apple India SWE New Grad Interviews: 5 Experiences
  • Apple New Grad SWE Phone Screens: 12 Experiences (Part 3 of 3)
  • Apple New Grad SWE and Embedded: 16 Experiences (Part 2 of 3)
All Apple interview questionsSee Apple hiring insights
Saved
class Solution {
    public int[][] shortestGridPath(int[][] costs, int[] start, int[] target) {
        // Write your code here.
    }
}
Checking account…