Do They Belong?
Reported by candidates from Goldman Sachs's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Goldman Sachs asked this in January 2024. You're given three points that form a triangle, and two query points p and q. You need to return a scenario number indicating whether each point is inside, on the boundary, or outside the triangle. This is a geometry problem that hinges on a single computational trick: the sign of the cross product. If you know how to check point-in-triangle using barycentric coordinates or the area method, you'll lock this down in under 10 minutes. StealthCoder can spot-check your logic if you freeze mid-implementation.
The problem
A point belongs to a triangle if it lies somewhere on or inside the triangle. Given two points p = (xp, yp) and q = (xq, yq), return the correct scenario number: Function Description Complete the function pointsBelong in the editor below. pointsBelong has the following parameter(s): int x1, y1, x2, y2, x3, y3: integer coordinates of the three points that may create a valid triangle int xp, yp, xq, yq: integer coordinates of the two points p and q Returns int: an integer value that represents the scenario
Reported by candidates. Source: FastPrep
Pattern and pitfall
The pattern is geometry. The standard approach is the signed area method: a point is inside or on a triangle if it's on the same side of all three edges. You compute the cross product (or signed area) for the point relative to each edge. If all three have the same sign, the point is inside or on the boundary. The common trap is handling the boundary case: points exactly on an edge count as 'belong'. You'll also need to handle the degenerate case where the three vertices are collinear (not a valid triangle). The scenario numbering isn't spelled out in the problem text, so you'll infer it from the test cases: likely scenario 1 is both inside, scenario 2 is one in and one out, scenario 3 is both outside, and scenario 4 is for invalid triangles. StealthCoder lets you validate your scenario logic against live test feedback if you're unsure.
Memorize the pattern. If you can't, run StealthCoder. The proctor sees the IDE. They don't see what's behind it.
You can drill Do They Belong? cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Goldman Sachs's OA.
Goldman Sachs reuses patterns across OAs. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Do They Belong? FAQ
What's the trick to checking if a point is inside a triangle?+
Use signed area or cross products. For each edge of the triangle, compute the cross product of the edge vector and the vector from edge start to your point. If all three cross products have the same sign (or are zero), the point is inside or on the boundary. If signs differ, it's outside.
How do I handle points on the boundary?+
Points on the boundary should return true (belong). The cross product method naturally handles this: if a point lies on an edge, the cross product for that edge is zero, and it still passes the test as long as the other two are consistent.
What if the three vertices are collinear?+
A collinear triangle is degenerate and not a valid triangle. You'll need to check this upfront by computing the signed area of the triangle. If it's zero, the triangle is invalid. The problem hints at scenario returns, so invalid triangles likely map to a specific scenario code.
How do I decode what the scenario numbers mean if they're not in the problem text?+
Run the first few test cases and reverse-engineer the output. You'll quickly see the pattern. Common schemes: scenario 1 (both in), 2 (one in one out), 3 (both out), 4 (invalid triangle). Adjust as test results reveal.
Can I solve this in under 10 minutes?+
Yes. The algorithm is straightforward once you recall the cross product method. Implement signed area checks for both points, handle the collinear case, and map results to scenario codes. Expect to spend 2-3 minutes coding, 5-7 minutes debugging boundary and degenerate cases.