Bloomberg Interview Question

C++ template metaprogramming (that team was into functional programming and compile time computations). Standard, find nth Fibonacci number using recursive templates.

Interview Answers

Anonymous

Oct 30, 2014

The obvious simple solution: #include template struct FIB { enum { RESULT = FIB::RESULT + FIB::RESULT }; }; template struct FIB { enum { RESULT = 0 }; }; template struct FIB { enum { RESULT = 1 }; }; int main() { std::cout ::RESULT << std::endl; return EXIT_SUCCESS; }

Anonymous

Apr 15, 2019

template struct fibonacci { using type = unsigned long int; static constexpr type value = fibonacci::value + fibonacci::value; }; template struct fibonacci { using type = unsigned long int; static constexpr type value = 0; }; template struct fibonacci { using type = unsigned long int; static constexpr type value = 1; }; int main() { return fibonacci::value; }