Write down a program to generate Fibonacci no series -This is yet another commonly asked problem in programing interviews.
Although there are multiple ways of solving this problem. Here I would show you the most simple method to do it. Also this method is efficient from a performance perspective.
public static List<int> GetFibonacciSeries(int number)
{
List<int> lstFibonacciNos = new List<int>();
int previousNo = 0, latestNo = 1;
switch (number)
{
case 0:
break;
case 1:
lstFibonacciNos.Add(previousNo);
break;
case 2:
lstFibonacciNos.Add(previousNo);
lstFibonacciNos.Add(latestNo);
break;
default:
lstFibonacciNos = FetchFibonacciNo(number);
break;
}
return lstFibonacciNos;
}
The description of Fibonacci series can be found here.
In the function GetFibonacciSeries() a number greater than zero is given as an input. For input numbers 1,2 it is an edge/boundary value scenario, that’s why we return hardcoded values. From number starting from 2 we do the actual calculations in FetchFibonacciNo() sub method shown below:
private static List<int> FetchFibonacciNo(int number)
{
List<int> lstFibonacciNos = new List<int>();
int previousNo = 0, latestNo = 1, newFibonacciNo = 0;
lstFibonacciNos.Add(previousNo);
lstFibonacciNos.Add(latestNo);
for (int i = 2; i < number; i++)
{
//adding previous two nos fetches the new Fibonacci series
newFibonacciNo = previousNo + latestNo;
lstFibonacciNos.Add(newFibonacciNo);
previousNo = latestNo;
latestNo = newFibonacciNo;
}
return lstFibonacciNos;
}
As you can see in the code above we initialize the first 2 nos in the series :
int previousNo = 0, latestNo = 1,
and we run a loop for getting the numbers in the series till nth term. The new number in the series fetched by adding last 2 previous numbers in the series as shown below:
newFibonacciNo = previousNo + latestNo;
The Unit test function(using Nunit) for the code is:
[Test, TestCaseSource(nameof(GetFibonacciSeriesTestCases))]
public void Test_GetFibonacciSeries(TestFibonacci testData)
{
//Arrange
//Act
var result = Num.GetFibonacciSeries(testData.No);
//Assert
Assert.AreEqual(testData.ExpectedResult, result,
"Fibonacci series has wrong result for the number :" + testData.No);
}
Some of the test scenarios are written in function (GetFibonacciSeriesTestCases) as shown below:
private static List<TestFibonacci> GetFibonacciSeriesTestCases()
{
List<TestFibonacci> lstFibonacciNos = new List<TestFibonacci>();
int[] fiboSeries = { 0 };
lstFibonacciNos.Add(GetFibonacciTestData(0, null));
lstFibonacciNos.Add(GetFibonacciTestData(1, fiboSeries));
fiboSeries = new int[] { 0, 1 };
lstFibonacciNos.Add(GetFibonacciTestData(2, fiboSeries));
fiboSeries = new int[] { 0, 1, 1 };
lstFibonacciNos.Add(GetFibonacciTestData(3, fiboSeries));
fiboSeries = new int[] { 0, 1, 1, 2 };
lstFibonacciNos.Add(GetFibonacciTestData(4, fiboSeries));
fiboSeries = new int[] { 0, 1, 1, 2, 3 };
lstFibonacciNos.Add(GetFibonacciTestData(5, fiboSeries));
fiboSeries = new int[] { 0, 1, 1, 2, 3, 5 };
lstFibonacciNos.Add(GetFibonacciTestData(6, fiboSeries));
fiboSeries = new int[] { 0, 1, 1, 2, 3, 5, 8 };
lstFibonacciNos.Add(GetFibonacciTestData(7, fiboSeries));
return lstFibonacciNos;
}
Perhaps readers might be thinking as to why I haven’t used recursive function to implement the solution. The thing is recursive function from a performance point of view is very expensive. That’s why it’s better to avoid recursion.
The source code can be found here :
Note: The project is in .NET 5 .Both the functions can found in Num.cs. Also Have a look into the unit test project(RandomUtilityTest) so that you can get an idea as to how to write better unit tests.
Leave a comment