# A little java challenge I was presented with today...

• 11-11-2011, 12:11 AM
A little java challenge I was presented with today...
Today on an interview I was presented with a challenge which can be applied to most high level languages I guess... I was unable to figure out how to solve it. So maybe you guys can give it a try.

The interviewer asked me if I could make a function or method that received an int as a parameter and returned another int.

If the function received a 7, it was supposed to return a 17. If it receive a 17 it should return a 7. In case it received any other int, it should return the number received.

Now that seems fairly easy, except that no if, loops or conditional expressions could be used to solve this, no try catch (exception treatment) either.

So I thought about it for a while, I thought either converting the number to strings and going from there, or maybe it's just a pure mathematical solution. Either way I wasn't able to find a solution.
• 11-11-2011, 12:14 AM
Junky
Re: A little java challenge I was presented with today...
Switch maybe? Technically it does not use a condition.
• 11-11-2011, 12:31 AM
javaTurtle
Re: A little java challenge I was presented with today...
Interesting.....well, Ithink this would work but only if the number returned didn't matter....

public int numberChange(int xInt){
Random rand = new Random();
return rand.nextInt() + xInt;
}

or some variation

Oh wait..forget that, I didnt see that it was passed a 7 in order to return a 17...scratch that code
...
• 11-11-2011, 02:19 AM
Re: A little java challenge I was presented with today...
sorry no switch statement either...
• 11-11-2011, 02:40 AM
Koala
Re: A little java challenge I was presented with today...
• 11-11-2011, 04:19 AM
Shoss
Re: A little java challenge I was presented with today...
I'm sure there's a much more elegant way to do this, but after half an hour this is all i could come up with. Relational operations are allowed?

Code:

```public int converter(int input) {         Boolean b = (input == 7) || (input == 17);             int newInput = -(input - 12) + 12;             int[] arr = {0, newInput, 0, 0, 0, 0, 0, input};                 return arr[b.hashCode()-1230];     }```
Edit: Nope, hashCode() uses a conditional.
• 11-11-2011, 05:10 AM
CHLim
Re: A little java challenge I was presented with today...
Finally got it after 48 lines of code. The key for me is to use strings and the indexOf method to construct if statements without actually using IF().
• 11-11-2011, 05:13 AM
Shoss
Re: A little java challenge I was presented with today...
But the indexOf uses if statements and loops, so i feel like that wouldn't count?
• 11-11-2011, 05:35 AM
CHLim
Re: A little java challenge I was presented with today...
from what i understand after reading the questions, it says that YOU (ie the person solving the question) cannot use if, loops or conditional expressions. That shouldn't include if,loops or conditional expressions that are implemented by methods provided by the java language.

Just my 2 cents

And Shoss, out of curiosity what does this part of your code do?
Code:

`return arr[b.hashCode()-1230];`
• 11-11-2011, 06:59 AM
Shoss
Re: A little java challenge I was presented with today...
Ha, something random i found in the Boolean class. I was hoping to find something relating true == 1 and false == 0. "true" always returns 1237 and "false" returns 1231, so if i subtract 1230 it'll give me the positions in the array i want.
• 11-11-2011, 08:08 AM
CHLim
Re: A little java challenge I was presented with today...
haha, ic. Thats interesting. And thank you.
• 11-11-2011, 08:36 AM