Minimum Steps to a Fibonacci Number
Reported by candidates from Virtu Financial's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The edge case that wrecks a naive solution on this Virtu Financial OA, reported in July 2025, is generating Fibonacci numbers only up to x and forgetting the one just above it. You're asked for the minimum steps to turn x into a Fibonacci number, moving by 1 each step. It's a nearest-value problem in disguise. x tops out at 1,000,000, so the list of Fibonacci numbers is tiny. If you're taking this OA in the next couple of days, the logic is short. The traps are at the boundaries. StealthCoder sits invisibly on your screen as a safety net if you blank mid-assessment.
The problem
Given an integer x, return the minimum number of steps required to change x into a Fibonacci number. In each step, you may either increment or decrement the current number by 1. The Fibonacci sequence is defined as follows: F[0] = 0 F[1] = 1 For every i >= 2, F[i] = F[i - 1] + F[i - 2]. The elements of this sequence are called Fibonacci numbers. Function minimumFibonacciSteps(x: int) → int Examples Example 1 x = 15 return = 2 The closest Fibonacci number is 13. Decrementing 15 twice reaches 13, so the minimum number of steps is 2. Example 2 x = 1 return = 0 1 is already a Fibonacci number, so no steps are needed. Example 3 x = 13 return = 0 13 is already a Fibonacci number, so no steps are needed. Constraints 0 <= x <= 1,000,000
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick: each step changes x by 1, so the answer is just the absolute distance to the nearest Fibonacci number. No search needed. Generate Fibonacci numbers until one passes x, which gives you about 30 values for a 1,000,000 limit. Then take the minimum of abs(x - f) across the list. The common pitfall is stopping the generation at the last value less than or equal to x. For x = 15 that gives 13, which works, but for x = 20 you'd miss 21 and return 7 instead of 1. Generate one value past x and check both sides. Also handle x = 0, which is F[0] and returns 0, and remember F[1] and F[2] are both 1, so duplicates are harmless. Linear scan over the list is fine. If the edge cases slip your mind under pressure, StealthCoder can hand you the loop while the proctor sees nothing.
The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.
You can drill Minimum Steps to a 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. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Virtu Financial's OA.
Virtu Financial reuses patterns across OAs. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Minimum Steps to a Fibonacci Number FAQ
How hard is Minimum Steps to a Fibonacci Number really?+
Easy. It's a nearest-value problem once you see that each step moves by 1. The Virtu Financial version is short, and the only real risk is an off-by-one when generating Fibonacci numbers. If you can write a loop and an abs, you can solve it.
What's the trick to this problem?+
Answer equals the minimum absolute difference between x and any Fibonacci number. Build the sequence until it exceeds x, include that first value above x, then take the min distance. No BFS or DP is needed because steps are unit moves in either direction.
Which edge cases break a naive solution?+
Stopping at the last Fibonacci number at or below x misses the next one above it. For x = 20, 21 is one step away but 13 is seven away. Also check x = 0 and x = 1, where the answer is 0 because both are already Fibonacci numbers.
What's the time complexity I should state?+
Effectively O(log x) to generate the Fibonacci numbers, since they grow exponentially. With x up to 1,000,000 there are only around 30 of them. Space is the same if you store them, or O(1) if you track the previous and current values while scanning.
How do I prepare for this in 48 hours?+
Write the solution once from scratch and test x = 0, 1, 2, 4, 15, 20 and 1,000,000. Then practice a couple of other nearest-value problems. Focus on boundary handling rather than new patterns, since this one is mostly about not missing the value just above x.