Implement gradient descent for a simple function from scratch in Python. Asked in the Phone Screen stage. This ML coding question was part of a 1-hour phone screen and was expected to take about 20 minutes.
Implement gradient_descent(initial_x, learning_rate, iterations) for f(x) = (x - 4)^2. Its derivative is 2 * (x - 4). Return the final x after exactly iterations updates using x = x - learning_rate * derivative(x). Return the result rounded to 10 decimal places.
def gradient_descent(initial_x, learning_rate, iterations):