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

1. Member
Join Date
Nov 2011
Posts
3
Rep Power
0

## 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.

2. ## Re: A little java challenge I was presented with today...

Switch maybe? Technically it does not use a condition.

3. Member
Join Date
Nov 2011
Location
Arizona
Posts
10
Rep Power
0

## 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
...
Last edited by javaTurtle; 11-11-2011 at 12:39 AM.

4. Member
Join Date
Nov 2011
Posts
3
Rep Power
0

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

sorry no switch statement either...

5. Member
Join Date
Oct 2011
Posts
22
Rep Power
0

6. ## 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?

Java 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.
Last edited by Shoss; 11-11-2011 at 04:22 AM.

7. Member
Join Date
Nov 2011
Posts
24
Rep Power
0

## 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().

8. ## 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?

9. Member
Join Date
Nov 2011
Posts
24
Rep Power
0

## 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?
Java Code:
`return arr[b.hashCode()-1230];`
Last edited by CHLim; 11-11-2011 at 06:03 AM.

10. ## 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. Member
Join Date
Nov 2011
Posts
24
Rep Power
0

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

haha, ic. Thats interesting. And thank you.

12. Member
Join Date
Nov 2011
Posts
3
Rep Power
0

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

I guess overloading is allowed, not sure about the use of functions that implement if, but the interviewer did not specify.

By the way shoss, that is a clever solution you got.
Last edited by riscado; 11-11-2011 at 08:44 AM.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•