Results 1 to 12 of 12
  1. #1
    riscado is offline Member
    Join Date
    Nov 2011
    Posts
    3
    Rep Power
    0

    Default 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. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,762
    Rep Power
    7

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

    Switch maybe? Technically it does not use a condition.

  3. #3
    javaTurtle is offline Member
    Join Date
    Nov 2011
    Location
    Arizona
    Posts
    10
    Rep Power
    0

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

  4. #4
    riscado is offline Member
    Join Date
    Nov 2011
    Posts
    3
    Rep Power
    0

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

    sorry no switch statement either...

  5. #5
    Koala is offline Member
    Join Date
    Oct 2011
    Posts
    22
    Rep Power
    0

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

    Is overloading allowed?

  6. #6
    Shoss's Avatar
    Shoss is offline Member
    Join Date
    Feb 2011
    Posts
    94
    Rep Power
    0

    Default 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 03:22 AM.

  7. #7
    CHLim is offline Member
    Join Date
    Nov 2011
    Posts
    24
    Rep Power
    0

    Default 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. #8
    Shoss's Avatar
    Shoss is offline Member
    Join Date
    Feb 2011
    Posts
    94
    Rep Power
    0

    Default 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. #9
    CHLim is offline Member
    Join Date
    Nov 2011
    Posts
    24
    Rep Power
    0

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

  10. #10
    Shoss's Avatar
    Shoss is offline Member
    Join Date
    Feb 2011
    Posts
    94
    Rep Power
    0

    Default 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
    CHLim is offline Member
    Join Date
    Nov 2011
    Posts
    24
    Rep Power
    0

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

    haha, ic. Thats interesting. And thank you.

  12. #12
    riscado is offline Member
    Join Date
    Nov 2011
    Posts
    3
    Rep Power
    0

    Default 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 07:44 AM.

Similar Threads

  1. Java Challenge [II]
    By [RaIdEn] in forum New To Java
    Replies: 10
    Last Post: 01-21-2010, 03:18 PM

Posting Permissions

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