WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Data Structures & Algorithms · Advanced · question 41 of 100

Can you explain the A* search algorithm and its applications in pathfinding and AI?

📕 Buy this interview preparation book: 100 Data Structures & Algorithms questions & answers — PDF + EPUB for $5

The A* search algorithm is a pathfinding algorithm used to find the shortest path between two nodes in a weighted graph. It is a variant of Dijkstra’s algorithm, but with the addition of a heuristic function that estimates the cost of the cheapest path from the current node to the goal node.

The algorithm works by maintaining a set of open nodes, representing nodes that still need to be explored, and a set of closed nodes, representing nodes that have already been explored. At each step, the algorithm selects the node in the open set with the lowest cost estimate, which is the sum of the cost of the path from the starting node to the current node and the heuristic estimate of the remaining cost to the goal node. The algorithm then expands the selected node and adds its neighbors to the open set if they have not been visited before or if a better path has been found.

The heuristic function used in A* is typically an admissible heuristic, meaning that it never overestimates the actual cost of reaching the goal node. One commonly used heuristic for pathfinding is the Manhattan distance, which calculates the distance between two nodes as the sum of the absolute differences between their x and y coordinates.

A* search algorithm is widely used in computer games and AI applications for pathfinding, as it is efficient and can handle complex environments with many obstacles and variable costs. It is also used in robotics for motion planning, in natural language processing for parsing, and in many other areas where finding the optimal path is important.

Here’s an example implementation of the A* search algorithm in Java:

public List<Node> aStarSearch(Node start, Node goal) {
    PriorityQueue<Node> openSet = new PriorityQueue<>();
    Set<Node> closedSet = new HashSet<>();
    Map<Node, Node> cameFrom = new HashMap<>();
    Map<Node, Double> gScore = new HashMap<>();
    Map<Node, Double> fScore = new HashMap<>();

    gScore.put(start, 0.0);
    fScore.put(start, heuristic(start, goal));
    openSet.add(start);

    while (!openSet.isEmpty()) {
        Node current = openSet.poll();
        if (current.equals(goal)) {
            return reconstructPath(cameFrom, current);
        }

        closedSet.add(current);
        for (Edge neighbor : current.getNeighbors()) {
            Node neighborNode = neighbor.getDestination();
            if (closedSet.contains(neighborNode)) {
                continue;
            }
            double tentativeGScore = gScore.get(current) + neighbor.getWeight();
            if (!openSet.contains(neighborNode) || tentativeGScore < gScore.get(neighborNode)) {
                cameFrom.put(neighborNode, current);
                gScore.put(neighborNode, tentativeGScore);
                fScore.put(neighborNode, tentativeGScore + heuristic(neighborNode, goal));
                if (!openSet.contains(neighborNode)) {
                    openSet.add(neighborNode);
                }
            }
        }
    }

    return null;
}

private double heuristic(Node node, Node goal) {
    return Math.abs(node.getX() - goal.getX()) + Math.abs(node.getY() - goal.getY());
}

private List<Node> reconstructPath(Map<Node, Node> cameFrom, Node current) {
    List<Node> path = new ArrayList<>();
    path.add(current);
    while (cameFrom.containsKey(current)) {
        current = cameFrom.get(current);
        path.add(0, current);
    }
    return path;
}

In this implementation, the aStarSearch method takes as input a starting node start and a goal node goal. The method initializes the open set with the starting node, and the closed set is initially empty. The heuristic method calculates the Manhattan distance between two nodes.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Data Structures & Algorithms interview — then scores it.
📞 Practice Data Structures & Algorithms — free 15 min
📕 Buy this interview preparation book: 100 Data Structures & Algorithms questions & answers — PDF + EPUB for $5

All 100 Data Structures & Algorithms questions · All topics