Reported February 2021
Bloombergdynamic programming

Fibonacci Number

Reported by candidates from Bloomberg's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live Bloomberg OA. Under 2s to a working solution.
Founder's read

The naive recursive Fibonacci is the version that dies on this Bloomberg OA, reported in February 2021. It looks like a warm-up, and it is, but n goes up to 91 and that single constraint is where people get burned. Plain recursion explodes in time, and the wrong integer type overflows quietly. You've got a trivial recurrence and two ways to fail it. If you freeze mid-assessment, StealthCoder runs invisibly on your desktop and hands you the iterative solution while the proctor sees nothing. Know the trick first, though. It takes five minutes.

The problem

Return the n-th Fibonacci number, where F(0)=0, F(1)=1, and F(n)=F(n-1)+F(n-2).

Function
fibonacci(n: int) → long

Examples
Example 1
n = 2
return = 1
F(2)=F(1)+F(0)=1.
Example 2
n = 10
return = 55
The tenth Fibonacci number is 55.

Constraints
0 <= n <= 91.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The pattern is dynamic programming at its simplest. Keep two variables, prev and curr, and roll them forward n times. That's O(n) time and O(1) space. The first pitfall is recursion without memoization, which is exponential and times out well before n hits 91. The second is overflow. F(91) is 4660046610375530309, which fits in a signed 64-bit integer but not a 32-bit one. The signature returns long, so use long or its equivalent in your language. Python doesn't care, but Java and C++ will. Handle n = 0 and n = 1 before the loop so you don't return garbage on the base cases. Test n = 0, n = 1, n = 2, and n = 91. If your mind blanks on the live OA, StealthCoder is the safety net that surfaces the loop and the type choice instantly.

Drill it cold or hedge it with StealthCoder. Either way, don't walk into the OA hoping you remember the trick.

If this hits your live OA

You can drill Fibonacci Number cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Made for the candidate who got the OA invite this morning and has 72 hours, not six months.

Get StealthCoder

Related leaked OAs

⏵ Practice the LeetCode equivalent

This OA pattern shows up on LeetCode as fibonacci number. If you have time before the OA, drill that.

⏵ The honest play

You've seen the question. Make sure you actually pass Bloomberg's OA.

Bloomberg reuses patterns across OAs. Made for the candidate who got the OA invite this morning and has 72 hours, not six months. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Fibonacci Number FAQ

How hard is the Bloomberg Fibonacci Number OA really?+

Easy. The recurrence is given in the statement. The only real risk is picking an approach that's too slow or an integer type that's too small. If you write an iterative loop with a 64-bit type, you're done in a few minutes.

What's the trick to this problem?+

Don't recurse naively. Track the last two values in variables and loop up to n. That avoids exponential blowup and uses constant memory. Memoized recursion also works, but the loop is shorter and harder to get wrong.

Why does the constraint stop at n = 91?+

Because F(92) overflows a signed 64-bit integer. The cap tells you the answer must fit in a long. It's a hint that overflow is the hidden edge case, so use a 64-bit type in Java, C++, or similar languages.

Which edge cases should I test before submitting?+

Test n = 0 (returns 0), n = 1 (returns 1), n = 2 (returns 1), n = 10 (returns 55), and n = 91 as the maximum. The first two catch base-case bugs. The last one catches overflow from using a 32-bit integer.

How do I prepare for this in 48 hours?+

Write the iterative version from memory twice, then the memoized recursive version once. Check the n = 91 case by hand for type size. Then spend your remaining time on harder dynamic programming problems, since this one is likely just a warm-up.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with Bloomberg.

OA at Bloomberg?
Invisible during screen share
Get it