Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Root Finding With Binary Search

EasyPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Root Finding With Binary Search. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

In a Siemens NX geometry workflow, a parameterized calculation may require finding where a monotonic function reaches zero. Implement a binary-search, or bisection, method to approximate the unique root of a cubic polynomial.

The polynomial is defined as f(x) = a*x^3 + b*x + c, where a > 0 and b >= 0. Given coefficients [a, b, c], an interval [lo, hi], an absolute tolerance, and a maximum iteration count, return an approximation to the root. The input guarantees that f(lo) <= 0 <= f(hi), so the root is within the interval. Return the result rounded to 6 decimal places.

Formal Specification

Implement root_binary_search(coefficients, lo, hi, tolerance, max_iterations).

  • coefficients is a list [a, b, c] of numbers.
  • lo, hi, and tolerance are floating-point numbers.
  • max_iterations is a positive integer.
  • Return a floating-point approximation to the unique x such that f(x) = 0.

At each iteration, evaluate the midpoint and retain the half-interval that still contains the sign change.

Constraints

  • a > 0 and b >= 0
  • -10^6 <= a, b, c <= 10^6
  • lo < hi
  • f(lo) <= 0 <= f(hi)
  • 1e-12 <= tolerance <= 1
  • 1 <= max_iterations <= 100000

Function Signature

def root_binary_search(coefficients, lo, hi, tolerance, max_iterations):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output