Slope of a Nonvertical Line
Updated
Problem statement
Given two points (x1,y1) and (x2,y2), return the real-valued slope of the straight line passing through them: (y2-y1)/(x2-x1).
For this exercise, assume x1 != x2, so the line is not vertical. Either point may be farther to the left. Return a floating-point number without truncating the quotient to an integer.
For this exercise, an answer is accepted when its absolute error is at most 1e-9 * max(1, abs(correctSlope)).
Function
lineSlope(x1: int, y1: int, x2: int, y2: int) → doubleExamples
Example 1
x1 = 4y1 = 2x2 = 2y2 = 5return = -1.5The vertical change is 5-2=3 and the horizontal change is 2-4=-2. Their ratio is -1.5; reversing the point order would give the same slope.
Example 2
x1 = 0y1 = 1x2 = 4y2 = 3return = 0.5The vertical change is 3-1=2 and the horizontal change is 4-0=4, so the slope is 0.5 rather than the integer-truncated value 0.
Constraints
- For this exercise, assume every coordinate is an integer between -1000000 and 1000000.
- The x coordinates differ: x1 != x2.
More Morgan Stanley Online Assessment questions
- Three-Resource 0/1 KnapsackOA · Seen Sep 2026
- Maximum Concurrent ProcessesOA · ONSITE INTERVIEW · Seen Sep 2026
- Maximum Total of Nonadjacent RatingsOA · Seen Jul 2026
- Count Shipment SchedulesOA · Seen Apr 2026
- Delete and EarnOA · Seen Apr 2026
- Interleave Array HalvesOA · Seen Apr 2026
- Get Patch SequenceOA · Seen Feb 2025
- String Subsequences 🐳OA · Seen Mar 2024
- Binary Tree Level OrderOA · Seen Feb 2024
- Merge IntervalsOA · Seen Feb 2024
Similar questions at other companies
- Overlapping Random Time WindowsSusquehanna International Group (SIG) · OASusquehanna International Group (SIG) interview questions
- Squared Frobenius Norm of a Reflect-and-Stretch MapWalleye Capital · OA
- Triangle and PointsGreyOrange · OA
- Circles RelationshipIBM · OAIBM interview questions
- Cyber Beacon DetectionRippling · OARippling interview questions
- Do They Belong?Goldman Sachs · OAGoldman Sachs interview questions
- Closest Point to a LineAdobe · OAAdobe interview questions
- Range of a Third CorrelationTower Research Capital · PHONE SCREEN
- Three Points in a Semicircle: Monte Carlo EstimateSquarepoint Capital · PHONE SCREENSquarepoint Capital interview questions
- Union Area of Two RectanglesDocuSign · PHONE SCREENDocuSign interview questions
Interview reports that mention this question
Morgan Stanley interview experiences
- Morgan Stanley Apprenticeship and Tech Interviews: 6 Experiences
- Morgan Stanley New Grad Technology Analyst: 2 Experiences
- Morgan Stanley Intern Oldest Loops: 5 Experiences (Part 5 of 5)
- Morgan Stanley Intern Earlier Loops: 5 Experiences (Part 3 of 5)
- Morgan Stanley Intern Tech Rounds: 5 Experiences (Part 2 of 5)