HackerRank: Find the Point

I finally found the math problems on HackerRank which makes me so happy. So let’s getting cracking on the first one called Find the Point. It asks us to

Consider two points,  and . We consider the inversion or point reflection, of point  across point   to be a  rotation of point  around .

Given  sets of points  and , find  for each pair of points and print two space-separated integers denoting the respective values of  and  on a new line.

They also give us some examples that we can study – the first example is the given as

Find the point - example1

I wont go through them here, but take a look at the original problem definition. I spent a good while trying to figure out what they meant and had to read the problem a couple of times. But as far as I understand they want us to mirror point p in point q and give that result as point r. That should be pretty easy.

Find the point through point reflection

The problem states pretty clearly, that there are some help to be gained on Wikipedia. When you look there, you will see that it is described through the use of vectors. So we can find the reflection point with the formula

That is to find the reflection point of a across the point p. so in this case a is our p and p is our q.

This is given as vectors, but that is basically the same as points, and in this case it is two dimensional. So we can write it as

Furthermore we can unwrap the vectors into the x and y component so we get

and that will be our solution.

Coding all that in Python is pretty straight forward. I have not included the whole code, since the problem already gives the skeleton and  just asks us to complete the function called findPoint. Doing that can be done as

def findPoint(px, py, qx, qy):
    return [2*qx - px, 2*qy - py]

This solves the problem fast and efficiently.

Posted by Kristian


Unless I’m very mistaken you got a typo there, the second “r” in the formula after the sentence “That is to find the reflection point of a across the point p. so in this case a is our p and p is our q.” should be a “p”.

You are right and thanks. It is changed now.

Leave a Reply