Google | Phone Interview | Points in 2D Plane
Anonymous User
2169

Problem:
Consider a 2D plane with some arbitrary points. Given some relations between different points. Return true if all the statements given are valid otherwise return false.

The input statements contains three attributes, which includes the two points and the relation between those points in terms of direction.
The directions can be one of [N, S, E, W, NE, NW, SE, SW]

For instance, the statement "P1 N P2" represent point P2 is to the North of point P1.
image

Example 1:

image

Input
P1 N P2
P2 N P3
P3 S P1

Output
true

Explanation
The first statement "point P2 is to the north of point P1" is valid, because that's the only statement we know about any of the points in the 2D plane.
The second statement "point P3 is to the north of P2" is also valid, as it does not contradicts with any of the previous statements.
The third statement "point P1 is to the south of P3" is also valid, by transitive property.

Input
P1 N P2
P2 N P3
P1 S P3

Output
false

Explanation
Here the third statement contradicts the relations of previous statements, therefore the output would be false.

Example 2:

image

Input
P1 N P2
P2 S P3
P1 E P3
P3 NW P4
P5 SW P4
P1 W P5

Output
true

Comments (9)