Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Colliding Spheres Function

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

Your question is Colliding Spheres 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

Magic Leap's spatial computing runtime tracks moving objects in 3D. Given spherical objects with constant velocities, return the earliest collision during a time window without simulating discrete frames.

For every unordered pair of spheres, determine the smallest t in [0, duration] at which the distance between their centers is less than or equal to the sum of their radii. Return [t, i, j] for the earliest collision, where i < j are the sphere indices. If multiple pairs collide at the same time, return the lexicographically smallest pair. Return None if no collision occurs.

An initially overlapping or touching pair collides at time 0. Treat floating-point comparisons within 1e-12 as equal.

Formal Specification

spheres is a list of spheres represented as [x, y, z, vx, vy, vz, radius], where all values are real numbers. duration is a nonnegative real number. Return either a list [collision_time, i, j] or None.

Do not use frame-by-frame simulation. Sphere centers follow linear trajectories: position(t) = position(0) + velocity * t.

Constraints

  • 2 <= len(spheres) <= 10^4
  • 0 <= duration <= 10^6
  • -10^6 <= x, y, z, vx, vy, vz <= 10^6
  • 0 < radius <= 10^6
  • Each sphere is represented as [x, y, z, vx, vy, vz, radius]
  • Return the earliest contact, not the time of deepest overlap
  • Floating-point comparisons within 1e-12 are considered equal

Function Signature

def earliest_sphere_collision(spheres, duration):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output