Search Algorithms — AI Technical Interview Questions & Answers
Search Algorithms
Search Algorithms
- Search finds a path from start to goal on a graph of states. Uninformed: BFS, DFS, UCS — no hint about the goal. Informed: greedy, A* — use a heuristic h(n). Adversarial: minimax when an opponent plays. Always say state, action, cost.
- Campus map: rooms as nodes, corridors as edges. BFS finds fewest doors if every door costs 1. A* if walking time differs. Don’t start coding before you draw the tiny graph.
- Search Algorithms — output: ['A', 'B', 'D'] (or A-C-D). Queue = fewest doors if each edge costs 1.
start
│
├── BFS (queue, fewest steps)
├── DFS (stack, deep first)
└── A* (g+h, if heuristic OK)
│
▼
goalTechnical Interview Tip: BFS vs A* in one sentence each.
Code Examples for Search Algorithms
Example
# BFS shortest hops
from collections import deque
graph = {"A": ["B", "C"], "B": ["D"], "C": ["D"], "D": []}
def bfs(start, goal):
q = deque([(start, [start])])
seen = {start}
while q:
node, path = q.popleft()
if node == goal:
return path
for nxt in graph[node]:
if nxt not in seen:
seen.add(nxt)
q.append((nxt, path + [nxt]))
return None
print(bfs("A", "D"))
Search Algorithms — output: ['A', 'B', 'D'] (or A-C-D). Queue = fewest doors if each edge costs 1.
How to Explain Search Algorithms to an Interviewer
Explain Search Algorithms as if you are teaching a junior — definition, then one example.
Uninformed vs informed vs adversarial. State + action + cost.
What does the example print, and what does that prove?
Search Algorithms — output: ['A', 'B', 'D'] (or A-C-D)
What mistake do freshers make with Search Algorithms?
Only the buzzword, no example