Programming Job Interview Challenge
I just found a very interesting Series on http://www.dev102.com.
They feature a Series about Questions in Job Interviews. There were already 3 Challenges.
Because the last part wasn’t that easy this part is all about simplicity.
This week’s challenege can be found here
The Task:
How would you implement the following method:
Foo(7) = 17 and Foo(17) = 7.
Any other input to that method is not defined so you can return anything you want. Just follow those rules:
- Conditional statements (if, switch, …) are not allowed.
- Usage of containers (hash tables, arrays, …) are not allowed.
The Answer:
So you need a function which takes in an integer value and returns a simple integer value.
This sound’s like a simple linear math function.
The function can be found relative easily.
f(7) = 17
f(17) = 7
so
7 + 17 = 24
17 + 7 = 24
There we have the connection between the value pairs so the calculation would be
f(x) = 24 – x;
in C# I would write the method as following:
int Foo(int x)
{
return 24 - x;
So this would be my solution for the task.
