Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Quadratic Formula Function

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

Your question is Quadratic Formula Function. 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

Flock Safety camera analytics can produce calibration equations that need to be solved reliably. Implement a function that resolves ax² + bx + c = 0, including degenerate and complex-valued cases.

Formal Specification

Write solve_quadratic(a, b, c), where a, b, and c are integers or floating-point numbers. Return a dictionary with a type field and a roots field:

  • type is one of "quadratic", "linear", "identity", or "none".
  • Each root is represented as [real_part, imaginary_part].
  • Return real roots with an imaginary part of 0.
  • Return roots in ascending lexicographic order by real part, then imaginary part.
  • For an identity equation, return an empty root list because every real number is a solution.
  • For an inconsistent equation, return an empty root list.

Use a numerically stable approach for two distinct real roots rather than directly evaluating the conventional formula twice.

Constraints

  • -10^9 <= a, b, c <= 10^9
  • At least one coefficient is nonzero
  • Return roots using floating-point values
  • Floating-point results are judged with tolerance 10^-9

Function Signature

def solve_quadratic(a, b, c):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output