Fibonacci Number with Constant Extra Space

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

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

The mistake that sinks a first attempt on this ZipRecruiter OA, reported in March 2017, is writing the textbook recursion and calling it done. The problem is Fibonacci with F(0)=0, F(1)=1, and n up to 91. The title says constant extra space, so the grader is watching for what you allocate. It's an easy problem with two or three ways to lose points. If you blank on the details live, StealthCoder is the invisible safety net that reads the prompt and hands you a clean solution. Know the trick before you start.

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 reduced to two variables. Keep prev and curr, start at 0 and 1, and loop n times, shifting them forward. That's O(n) time and O(1) space, which is exactly what the title asks for. The pitfall is naive recursion. It's exponential and will time out well before n hits 91. The second pitfall is an array or memo table, which breaks the constant space requirement. The third is overflow. F(91) is about 4.66e18, which fits in a signed 64-bit long, but F(92) doesn't. So use a 64-bit type and don't go past n. Handle n=0 and n=1 cleanly by returning n early. If you freeze during the live OA, StealthCoder is the hedge that gives you the loop version fast, but this one is short enough to memorize tonight.

Memorize the pattern. If you can't, run StealthCoder. The proctor sees the IDE. They don't see what's behind it.

If this hits your live OA

You can drill Fibonacci Number with Constant Extra Space 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 by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge.

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 ZipRecruiter's OA.

ZipRecruiter reuses patterns across OAs. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Fibonacci Number with Constant Extra Space FAQ

What's the trick in the ZipRecruiter Fibonacci OA question?+

Don't recurse and don't store a table. Keep two variables, prev=0 and curr=1, and iterate n times updating them. That gives O(n) time and O(1) space, which matches the constant extra space in the title.

Why is the constraint n <= 91?+

F(91) is roughly 4.66e18, the largest Fibonacci value that fits in a signed 64-bit integer. F(92) overflows. So the limit tells you a 64-bit long is enough and you don't need big integers.

How hard is this problem really?+

Easy. The logic is five lines. People lose points by using exponential recursion, allocating an O(n) array, or mishandling n=0. If you write the two-variable loop and test n=0, 1, 2, and 10, you're fine.

Should I use matrix exponentiation or fast doubling?+

You don't need to. With n capped at 91, a linear loop is instant. Fast doubling gives O(log n) but adds bug risk for no gain here. Pick the simple loop unless the prompt asks for logarithmic time.

How do I prepare for this in 48 hours?+

Write the iterative version from memory three times. Check n=0 returns 0, n=1 returns 1, n=2 returns 1, n=10 returns 55. Confirm you use a 64-bit type. That's the whole prep for this question.

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

OA at ZipRecruiter?
Invisible during screen share
Get it