Results 1 to 12 of 12
- 11-10-2011, 11:11 PM #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.
- 11-10-2011, 11:14 PM #2
Re: A little java challenge I was presented with today...
Switch maybe? Technically it does not use a condition.
- 11-10-2011, 11:31 PM #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-10-2011 at 11:39 PM.
- 11-11-2011, 01:19 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...
- 11-11-2011, 01:40 AM #5
Member
- Join Date
- Oct 2011
- Posts
- 22
- Rep Power
- 0
Re: A little java challenge I was presented with today...
Is overloading allowed?
- 11-11-2011, 03:19 AM #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?
Edit: Nope, hashCode() uses a conditional.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]; }Last edited by Shoss; 11-11-2011 at 03:22 AM.
- 11-11-2011, 04:10 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().
- 11-11-2011, 04:13 AM #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?
- 11-11-2011, 04:35 AM #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 05:03 AM.
- 11-11-2011, 05:59 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-11-2011, 07:08 AM #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.
- 11-11-2011, 07:36 AM #12
Member
- Join Date
- Nov 2011
- Posts
- 3
- Rep Power
- 0
Similar Threads
-
Java Challenge [II]
By [RaIdEn] in forum New To JavaReplies: 10Last Post: 01-21-2010, 03:18 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks