Optimal strong approximation for quadratic forms