Prepin
Log in

Determine whether two lines are parallel.

Approach / Explanation

In this question, the interviewer expects you to showcase your understanding of fundamental geometry and basic mathematics. You will specifically need to determine if two lines are parallel. From a geometric standpoint, parallel lines are defined as lines in a plane that do not intersect; they do not meet at any point and are not perpendicular to one another. They maintain a constant distance apart throughout their entire length. In a programming context, you will likely receive the equations for two lines expressed in terms of their slope and y-intercept. These equations will all be linear and can be represented in the format y = mx + b, where m indicates the slope, b is the y-intercept, and (x, y) represents the coordinates of a point on the line. The key principle for determining if two lines are parallel is that they will have the same slope . Thus, your algorithm should focus on calculating and comparing the slopes of the two provided lines. If your response includes these essential steps, you will make a positive impression on the interviewer: Obtain or compute the slopes of the given lines. Compare the slopes. If they are the same, the lines are parallel; if they differ, they are not parallel.

Suggested Answer

For two lines defined by their slopes and y-intercepts (m1, b1) and (m2, b2), we can create an algorithm as follows: def are_lines_parallel ( m1 , m2 ) : if m1 == m2 : return True else : return False This solution assumes that the slopes of the lines have already been determined or provided, and it simply compares them. If they are equal, the function returns True, indicating that the lines are parallel; otherwise, it returns False, indicating they are not parallel.

Alternative Answer

When provided with two points, (x1, y1) and (x2, y2), for each line, we can begin by calculating the slope using the formula slope = (y2 - y1) / (x2 - x1). Here’s the process: def calculate_slope ( x1 , y1 , x2 , y2 ) : if x2 - x1 == 0 : # to prevent division by zero error return float ( 'inf' ) else : return ( y2 - y1 ) / ( x2 - x1 ) def are_lines_parallel ( line1 , line2 ) : slope1 = calculate_slope ( * line1 ) slope2 = calculate_slope ( * line2 ) if slope1 == slope2 : return True else : return False In this example, we calculate the slopes of both lines and then compare them. This function handles the scenario where the denominator could be zero, which occurs for vertical lines. In such cases, it returns infinity, as the slope of a vertical line is considered undefined (or infinite for this specific context). The primary function, 'are_lines_parallel', invokes the 'calculate_slope' function to find the slope of each line, subsequently comparing the slopes. If the slopes are the same, the lines are parallel, resulting in a return value of True; otherwise, it returns False.

Question Details

Difficulty & Category

Medium
algorithms

Product

For CandidatesFor RecruitersCompaniesBrowse JobsInterview QuestionsDeep ProfileSkill AssessmentOpportunity Matching
Prepin.ai

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