Hill Climbing — AI Technical Interview Questions & Answers
Hill Climbing
Hill Climbing
- Hill climbing looks only at neighbours and steps to a better one. Like climbing in fog: you go up, then you stop at a peak that may not be the highest mountain. Local maxima, plateaus, ridges trap it. Variants: random restart, simulated annealing.
- 8-queens or a tiny landscape sketch is enough. Don’t claim hill climbing always finds global best. It is greedy local search, not A*.
- Hill Climbing — output: stopped at index 1 height 5. Global peak is 9. Local maximum trap.
Technical Interview Tip: Local max trap + one fix.
Code Examples for Hill Climbing
Example
# Hill climb — may miss global peak
height = [2, 5, 4, 9, 8]
i = 0
while i + 1 < len(height) and height[i + 1] > height[i]:
i += 1
print("stopped at index", i, "height", height[i])
Hill Climbing — output: stopped at index 1 height 5. Global peak is 9. Local maximum trap.
How to Explain Hill Climbing to an Interviewer
Explain Hill Climbing as if you are teaching a junior — definition, then one example.
Step to better neighbour. Local maximum.
What does the example print, and what does that prove?
Hill Climbing — output: stopped at index 1 height 5
What mistake do freshers make with Hill Climbing?
Fix : random restart.