Today I had an interview for a developer job and I had to do this aptitude test…
I got stuck one 2 questions:
box 1 box 2 box 3
A B C
How to put A in each boxes with only those 4 operator (ADD, SUB,MULT and DIV)?
box 1 box 2 box 3
You have the numbers 1,2 and 3 that can be in either box1 box2 or box3 but we don’t know in which is which… How to put 7 in box 3 ?
Can someone can explain me how to do it ?
ps: syntax is: ADD,1,2,3 => box3 = box1 + box2
Thank you
For the first question, you can set boxes B and C equal to 2A by just storing A + A in each:
Now, just subtract A from each box:
This results in A stored everywhere. The resulting program is thus
For the second question, one cute trick would be to divide some box by its own value to put 1 into it. For example:
Now, let’s put 2 into box 2 by doubling the contents of box 1 and storing it there:
Now, let’s put 4 into box 3 by doubling the contents of box 2 and storing it there:
Next, add 2 to box three by adding in the contents of box 3. This makes box 3 hold 6:
Finally, add 1 to box three by adding in the contents of box 3. This makes box 3 holds 7:
The total program is thus
Hope this helps!