Prepin
Log in

Determine the distance between two points in three-dimensional space.

Approach / Explanation

This inquiry seeks an algorithm to determine the distance between two points located in three-dimensional (3D) space. The coordinates of the two points are given as (x1, y1, z1) and (x2, y2, z2). The algorithm should compute and return the Euclidean distance between these points. To provide a comprehensive response, keep in mind the following: Recognize the context: the issue pertains to a three-dimensional space, meaning all points are in 3D. Derive the Euclidean distance formula for 3D: The distance d between two points in 3D can be calculated using the formula: d = sqrt((x2-x1)² + (y2-y1)² + (z2-z1)²). Develop an algorithm that accurately applies this formula.

Suggested Answer

A Python function designed to compute the distance between two points in 3D space can be structured as follows: import math def calculate_distance(point1, point2): x1, y1, z1 = point1 x2, y2, z2 = point2 return math.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2 + (z2 - z1) ** 2) This function accepts two tuples that represent the coordinates of the points in 3D space, and it calculates and returns the Euclidean distance between them.

Alternative Answer

Alternatively, you can implement a solution in Java like this: public double calculateDistance(double[] point1, double[] point2) { double xDiff = point2[0] - point1[0]; double yDiff = point2[1] - point1[1]; double zDiff = point2[2] - point1[2]; return Math.sqrt(xDiff * xDiff + yDiff * yDiff + zDiff * zDiff); } This Java method represents the two points as arrays of doubles. It computes the differences between the respective coordinates of the points, squares these differences, adds them together, and then takes the square root of the total to determine the Euclidean distance. The result is returned as a double value.

Question Details

Difficulty & Category

Medium
algorithms

Companies

Apple

Product

For CandidatesFor RecruitersCompaniesBrowse JobsInterview QuestionsDeep ProfileSkill AssessmentOpportunity Matching
Prepin.ai

© 2026 Prepin LLC · Philadelphia, PA · All rights reserved.