views:

163

answers:

1

This programming problem is #85 from this page of Microsoft interview questions (http://halcyon.usc.edu/~kiran/msqs.html). The complete problem description and my solution are posted below, but I wanted to ask my question first.

The rules say that you can loop for a fixed number of times. That is, if 'x' is a variable, you can loop over a block of code 'based on the value of 'x' at the time that you enter the loop. If 'x' changes during the loop, that won't change how many times you loop. Also, that is the only way to loop. You can't, for instance, loop until some condition is met.

In my solution to the problem, I have a loop which will be set to execute zero or more times. The problem is, in reality, it only ever executes 0 times or 1 time because the only statement in my loop is a return statement. So if we enter the loop, it only has a chance to run once. Basically, I am using this tactic instead of using an if-else block (because logical comparisons and if's are not allowed). The rules don't explicitly say that you can't do this, but I am wondering if you would consider my solution invalid. I couldn't really figure out another way to do it.

So her eare my questions: Do you think my solution is invalid? If so, did you think of another way to solve the problem?

Problem description:

85.You have an abstract computer, so just forget everything you know about computers, this one only does what I'm about to tell you it does. You can use as many variables as you need, there are no negative numbers, all numbers are integers. You do not know the size of the integers, they could be infinitely large, so you can't count on truncating at any point. There are NO comparisons allowed, no if statements or anything like that. There are only four operations you can do on a variable.
1) You can set a variable to 0.
2) You can set a variable = another variable.
3) You can increment a variable (only by 1), and it's a post increment.
4) You can loop. So, if you were to say loop(v1) and v1 = 10, your loop would execute 10 times, but the value in v1 wouldn't change so the first line in the loop can change value of v1 without changing the number of times you loop.
You need to do 3 things.
1) Write a function that decrements by 1.
2) Write a function that subtracts one variable from another.
3) Write a function that divides one variable by another.
4) See if you can implement all 3 using at most 4 variables. Meaning, you're not making function calls now, you're making macros. And at most you can have 4 variables. The restriction really only applies to divide, the other 2 are easy to do with 4 vars or less. Division on the other hand is dependent on the other 2 functions, so, if subtract requires 3 variables, then divide only has 1 variable left unchanged after a call to subtract. Basically, just make your function calls to decrement and subtract so you pass your vars in by reference, and you can't declare any new variables in a function, what you pass in is all it gets.

My psuedocode solution: loop(x) means loop through this block of code x times

       //returns number - 1
    int decrement(int number)
    {
        int previous = 0;
        int i = 0;

        loop(number)
        {
            previous = i;

            i++;
        }

        return previous;
    }

    //returns number1 - number2
    int subtract(int number1, int number2)
    {
        loop(number2)
        {
            number1= decrement(number1);
        }

        return number1;
    }


    //returns numerator/denominator
    divide(int numerator, int denominator)
    {
        loop(subtract(numerator+1, denominator))
        {
            return (1 + divide(subtract(numerator, denominator), denominator));
        }

return 0;
    }

Here are C# methods that you can build and run. I had to make an artificial way for me to satisfy the looping rules.

public int decrement(int num)
    {
        int previous = 0;

        int LOOP = 0;

        while (LOOP < num)
        {
            previous = LOOP;

            LOOP++;
        }

        return previous;
    }

    public int subtract(int number1, int number2)
    {
        int LOOP = 0;

        while (LOOP < number2)
        {
            number1 = decrement(number1);

            LOOP++;
        }

        return number1;
    }

    public int divide(int numerator, int denominator)
    {
        int LOOP = 0;

        while (LOOP < subtract(numerator+1, denominator))
        {
            return (1 + divide(subtract(numerator, denominator), denominator));
        }

        return 0;
    }
A: 

Ok so the reason I think your answer might be invalid is because of how you use return. In fact I think just using the return is too much of an assumption. in a few places you use the return value of a function call as an extra variable. Now if the return statement is ok, then your answer is valid. The reason i think its not valid is because the problem hints at the fact that you need to think of these as macros, not function calls. Everything should be done by reference, you should change the variables and the return values are how you left those variables. Here is my solution:

 
int x, y, v, z;

//This function leaves x decremented by one and v equal to x
def decrement(x,v):
    v=0;
    loop(x):
        x=v;
        v++;

//this function leaves x decremented by y (x-y) and v equal to x
def subtract(x,y,v):
    loop(y):
        decrement(x,v);

//this function leaves x and z as the result of x divided by y, leaves y as is, and v as 0
//input of v and z dont matter, x should be greater than or equal to y or be 0, y should be greater than 0.
def divide(x,y,v,z):
    z=0;
    loop(x):
        //these loops give us z+1 if x is >0 or leave z alone otherwise
        loop(x):
            z++;
        decrement(x,v);
        loop(x):
            decrement(z,v)
        //restore x
        x++;
        //reduce x by y until, when x is zero z will no longer increment
        subtract(x,y,v);
    //leave x as z so x is the result
    x=z;



sibtx13