Point Inside a Triangle
Reported by candidates from SambaNova Systems's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The edge case that breaks a naive solution here is the point sitting exactly on a side, and SambaNova Systems reportedly asked this in June 2022. You get three integer vertices and one point, and you return whether the point is inside. Edges and vertices count as inside. The vertices can come in clockwise or counterclockwise order, which trips people up. It's a geometry problem with a cross-product trick underneath. If you know the sign test, it's ten lines. If you blank on it, StealthCoder is the safety net running invisibly during the live OA.
The problem
Given three non-collinear integer triangle vertices and one integer point, return whether the point lies inside the triangle. A point on an edge or vertex counts as inside. The vertices may be clockwise or counterclockwise. Function isPointInsideTriangle(triangle: int[][], point: int[]) → boolean Examples Example 1 triangle = [[0,0],[5,0],[0,5]] point = [1,1] return = true The point lies strictly inside. Example 2 triangle = [[0,0],[5,0],[0,5]] point = [3,3] return = false The point lies beyond the hypotenuse. Example 3 triangle = [[0,0],[5,0],[0,5]] point = [0,2] return = true An edge point counts as inside. Constraints triangle.length == 3, every coordinate row has length 2, and point.length == 2. Coordinates are between -10^9 and 10^9. The triangle has nonzero area.
Reported by candidates. Source: FastPrep
Pattern and pitfall
Use cross products. For each edge (A to B), compute cross(B-A, P-A). If the point is inside or on the boundary, all three signs are either all non-negative or all non-positive. Mixed strictly positive and strictly negative values mean outside. That handles clockwise and counterclockwise input without figuring out orientation first. A zero means the point is collinear with that edge, which is allowed. The big pitfall is overflow. Coordinates reach 10^9, so differences reach 2*10^9 and products reach 4*10^18 each, with a subtraction of two of them going past a signed 64-bit limit in some languages. Use 128-bit types, BigInt, or Python ints. Floating point and area-comparison approaches are also risky for the same reason. Don't use division. If you freeze on the sign logic during the live OA, StealthCoder can hand you the working version quietly.
Drill it cold or hedge it with StealthCoder. Either way, don't walk into the OA hoping you remember the trick.
You can drill Point Inside a Triangle 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 for the candidate who got the OA invite this morning and has 72 hours, not six months.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass SambaNova Systems's OA.
SambaNova Systems reuses patterns across OAs. Made for the candidate who got the OA invite this morning and has 72 hours, not six months. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Point Inside a Triangle FAQ
What's the trick to Point Inside a Triangle?+
Compute the cross product of each edge with the vector from its start to the point. If all three results are non-negative or all non-positive, the point is inside or on the boundary. Mixed strict signs mean outside. It works for either vertex orientation.
How do I handle points on an edge or vertex?+
Treat a zero cross product as acceptable, not as a failure. Only reject when you see both a strictly positive and a strictly negative value. A vertex gives zeros on two edges, and an edge point gives zero on one, so both pass naturally.
Do I need to worry about integer overflow?+
Yes. Coordinates go up to 10^9 in magnitude, so coordinate differences reach 2*10^9 and each product reaches 4*10^18. The subtraction of two products can exceed a signed 64-bit range. Use Python ints, BigInt, or 128-bit integers where available.
Why not use the area comparison method?+
Comparing the sum of three sub-triangle areas to the full area works in theory, but it needs absolute values and has the same overflow risk. It's also easier to get wrong with edge points. The sign-of-cross-product approach is shorter and cleaner.
How do I prepare for this in 48 hours?+
Write the cross product function from memory, then test it on the three examples plus a vertex, a collinear point outside the triangle, and a clockwise triangle. Check large-coordinate inputs too. That covers nearly every way this problem goes wrong.