A* Search Algorithm — AI Technical Interview Questions & Answers

A* Search Algorithm

A* Search Algorithm

  • A* expands the node with smallest f = g + h. g is cost so far. h is a guess of cost to goal. If h never overestimates (admissible) and is consistent, A* finds an optimal path. Straight-line distance on a map is the classic admissible h.
  • If h = 0, A* becomes UCS. If h is wild and overestimates, you may get a fast but non-optimal route. Viva: write f=g+h, say admissible, give straight-line example.
  • A* Search Algorithm — output: S f= 7 / A f= 6 / B f= 6. Expand smallest f. Admissible h never overestimates.
  f(n) = g(n) + h(n)
  pick smallest f
  admissible h → optimal

Technical Interview Tip: f=g+h + admissible heuristic example.

Code Examples for A* Search Algorithm

Example

# A* f = g + h
g = {"S": 0, "A": 2, "B": 5}
h = {"S": 7, "A": 4, "B": 1, "G": 0}
for n in ["S", "A", "B"]:
    print(n, "f=", g[n] + h[n])

A* Search Algorithm — output: S f= 7 / A f= 6 / B f= 6. Expand smallest f. Admissible h never overestimates.

How to Explain A* Search Algorithm to an Interviewer

Explain A* Search Algorithm as if you are teaching a junior — definition, then one example.

f(n) = g(n) + h(n). Admissible h → optimal.

What does the example print, and what does that prove?

A* Search Algorithm — output: S f= 7 / A f= 6 / B f= 6

What mistake do freshers make with A* Search Algorithm?

Overestimate h.