Oracle Interview Question
Software Engineer / Developers4224696333392304878706725602341482782579852840250681098010280137314308584370130707224123599639141511088446087538909603607640194711643596029271983312598737326253555802606991585915229492453904998722256795316982874482472992263901833716778060607011615497886719879858311468870876264597369086722884023654422295243347964480139515349562972087652656069529806499841977448720155612802665404554171717881930324025204312082516817125
The following code will do in Java. Perhaps, question is a good eye opener..
import java.math.BigDecimal;
public class Fib_2000 {
public static void main(String[] args)
{
BigDecimal value = fib(2000);
System.out.println(" Fibnocci value " + value);
}
public static BigDecimal fib(int x)
{
BigDecimal a = new BigDecimal(0);
BigDecimal b = new BigDecimal(1);
BigDecimal sum = new BigDecimal(0);
for(int i = x; i >=2; i--)
{
sum = a.add(b);
a = b;
b = sum;
}
return sum;
}
}
Yeah, awesome indeed.
The frustrated user will file a perf bug (this will not complete in their lifetime) instead of the eventual correctness bug (this will give wrong results due to integer overflow).
So you can claim it is slow, but you cannot claim it is wrong! HAHA.
Python code (Ha! Got you there, smart alec. Python integers are unlimited).
output
There is also O(log n) arithmetic operation algo (note, arithmetic operations are not O(1) if integers get unbounded), but was too lazy to code that up.
- Anonymous November 22, 2013