Oracle Interview Question
Software Engineer / Developers<pre lang="c++" line="1" title="CodeMonkey2970" class="run-this">#include <stdio.h>
int main()
{
int n = 1000;
int a,b;
a = n >> 2;
b = (a >> 2);
a += b;
b = ((b+2)>>2);
a += b;
b = ((b+2)>>2);
a += b;
b = ((b+2)>>2);
a += b;
printf("a=%d\n", a);
return 0;
}
</pre><pre title="CodeMonkey2970" input="yes">
</pre>
int k = 15;
double value = log((double)k/3)/log((double)k);
int val2 = ceil(pow(k, value));//output = 2;
Umm what about something like this:
num = log10(pow(pow(10,num),0.3333333333));
Obviously this doesnt work for large numbers. One can choose a smaller number to use as the base but there will still be limitations and (unless you use 'e') you will have to use division to divide by the log of that number.
God , please save me from these guys!
i think question clearly say not to use any arthimetic operations.
This can be achieved by 2 functions, one as the base addition, the other as the reverse of addition (i.e. subtraction) by multiple times.
public int add(int a, int b) {
if (b == 0)
return a;
int sum = a ^ b;
int carry = (a & b) << 1;
return add(sum, carry);
}
//suppose a can be wholly divided by b
public int division(int a, int b) {
int i = 0;
for (; a > 0; ) {
a = add(a, -b);
i = add (i,1);
}
return i;
}
<pre lang="" line="1" title="CodeMonkey36734" class="run-this">/* The class name doesn't have to be Main, as long as the class is not public. */
class Main
{
public static void main (String[] args) throws java.lang.Exception
{
java.io.BufferedReader r = new java.io.BufferedReader (new java.io.InputStreamReader (System.in));
String s;
while (!(s=r.readLine()).startsWith("42")) System.out.println(s);
}
}
</pre><pre title="CodeMonkey36734" input="yes">1</pre>
If we could use itoa(int n, int radix), we could convert it to base 3 and then drop the last letter of the string.
- NewBieFresher August 11, 2010Then convert the string back to number (atoi()) and then convert the radix-3 back to radix 10 using standard procedures. (or please suggest a library function that does that if you know one).