Practice Real Company Assessment Patterns • Latest 2025–2026 Mock Tests

Programming Exercises

Write a function that implements the A* search algorithm to find the shortest path in a grid.

Hard

Problem Statement

Write a function that implements the A* search algorithm to find the shortest path in a grid.

Sample Input

grid = [ [ 0, 0, 1, 0 ], [ 0, 1, 0, 0 ], [ 0, 0, 0, 0 ] ]
start = [ 0, 0 ]
end = [ 2, 3 ]

Sample Output

The shortest path as an array of coordinates (if exists) or an empty array if no path is possible.

Limits

Time: 2s · Memory: 256 MB

Code Editor
00:00
// Implement the A* search algorithm for shortest path in a grid function aStar(grid, start, end) { // Define heuristic function // Initialize open and closed sets // Loop to find shortest path using A* algorithm } console.log(aStar([ [ 0, 0, 1, 0 ], [ 0, 1, 0, 0 ], [ 0, 0, 0, 0 ] ], [ 0, 0 ], [ 2, 3 ]));
Log in or sign up to run or submit your code.Log inSign up
Input
grid = [ [ 0, 0, 1, 0 ], [ 0, 1, 0, 0 ], [ 0, 0, 0, 0 ] ]
start = [ 0, 0 ]
end = [ 2, 3 ]
Expected Output
The shortest path as an array of coordinates (if exists) or an empty array if no path is possible.