Тёмный

Fibonacci Series: #1 Most Common Coding Interview Question - Whiteboard Thursday 

Irfan Baqui
Подписаться 32 тыс.
Просмотров 79 тыс.
50% 1

Check out the detailed data structures and algorithms course at www.interviewa...
Today I cover the #1 most asked coding question in interviews. This problem throws off a lot of folks, so I go through both a recursive and an iterative implementation.
Please like and subscribe, and I'll see you next week!

Опубликовано:

 

7 сен 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 37   
@danpluso
@danpluso 5 лет назад
I coded up an iterative algorithm before watching the video and here is what I came up with. It's a little bit of a different approach with the first two cases not being as easy to understand compared to just using an if(n
@dishanx
@dishanx 6 лет назад
int Fib(int pos){ if (pos == 0) return 0; if (pos == 1) return 1; return Fib(pos - 1) + Fib(pos - 2); }
@DharmaTruthDuty
@DharmaTruthDuty 5 лет назад
Perhaps begin the video with a description of the actual question... otherwise great video !
@sriamit84
@sriamit84 4 года назад
Great video, only thing what i observed is, It should return last+sLast , because fib(5) would be 8 but in your case it will return 5.
@mortezaebrahimi3365
@mortezaebrahimi3365 4 года назад
Great channel. Thank you Irfan. Your videos had been really useful to me. Knowing the algorithms is completely different from how you do them in white-boarding interviews. And that is what I've learned from your videos. Thank you.
@vivektadpatri7413
@vivektadpatri7413 3 года назад
With Binet's formula, it is much faster, although I wouldn't expect the candidate to come up with this during the interview. After all, we are programmers, not mathematicians. Nice video.
@ltogan1
@ltogan1 5 лет назад
Assuming pos >= 0, then def fib(pos): if pos < 2 return pos values = [0, 1] val_pos = 1 for _ in range(pos - 2): val_pos = abs(val_pos - 1) values[val_pos] = values[0] + values[1] return values[pos]
@gavinthomasmagee525
@gavinthomasmagee525 6 лет назад
def find_nth_fibb_num(n): if n
@rufankhokhar7888
@rufankhokhar7888 5 лет назад
Sir i think this is the best solution to find number at position. int fibNumberAt(int pos) { int f1 = 0; int f2 = 1; int n = 0; while ( pos - - >1) { n = f1 + f2; f1 = f2; f2 = n; } return n; }
@sparkie65535
@sparkie65535 4 года назад
nice try :) fibNumberAt(1);
@gamebrot2813
@gamebrot2813 6 лет назад
Int x=1,y=1,s=0,n; cin>>n; For(int i=0;i
@TheSkeef79
@TheSkeef79 3 года назад
Actually, you can solve this problem in O(log(n)) time using matrices and binary exponentiation ( I don't know us it necessary in interviews)
@HenrikRuep
@HenrikRuep 6 лет назад
you can also get constant space, logarithmic time by using fast_expon for matrices. Note that the Fibonacci-recursion is given by [1,1;1,0]*[F_n;F_{n-1}]=[F_{n+1},F_n]. And now you just have to raise that matrix to the appropiate power.
@starman9000
@starman9000 4 года назад
here is my solution in c# public IEnumerable GetFibonacci(int number) { int prev = 1; int next = -1; for (int i = 0; i < number; i++) { int sum = prev + next; next = prev; prev = sum; yield return sum; } }
@ashmitsingh5473
@ashmitsingh5473 4 года назад
You are doing such a great job 👍👍
@asadanees781
@asadanees781 4 года назад
Hi Irfan, The explanation is in enough and complete. You should clearly and correctly explain.
@vdesidesi7435
@vdesidesi7435 6 лет назад
Thank you for the clear explanations, can you pls also make series based on company like amazon, facebook etc.
@steftasticgaming6305
@steftasticgaming6305 4 года назад
Honestly, I do net get the recursive method. I mean that f[pos-1] +f[pos-2] implies that we know the numbers before pos, right? And how do we know them when nothing in the code above implies that we have calculated them already and saved them in the array. Can somebody elaborate on that? Thanks in advance
@kristiangoranov
@kristiangoranov 2 года назад
Look up recursive Fibonacci. They don't know the numbers until it gets to the base case and works its way back up. And storing it in the array avoids recalculating the same numbers over and over.
@SumedhSen97
@SumedhSen97 4 года назад
Can it be assumed that questions for whiteboard interviews would be simple, so that focus is on how you approach a problem?
@datbui5863
@datbui5863 6 лет назад
Hi Irfan, Your videos are awesome! It would be nice if you provide task/condition on a separate slide at the beginning of a video.
@IrfanBaqui
@IrfanBaqui 6 лет назад
that's a good idea, thanks for the feedback.
@josueh.ramirez7643
@josueh.ramirez7643 4 года назад
Would this be the same concept when using maps --???
@IrfanAli-so5hh
@IrfanAli-so5hh 4 года назад
Hii man.. We both have same names !!! 😉
@guitarockdude
@guitarockdude 5 лет назад
Good video, one thing though. I think you're returning the wrong value at the end. You are returning the "last" value, but you should be returning "last + secondLast" no?
@pigeot911
@pigeot911 5 лет назад
Noo its last value :)
@danpluso
@danpluso 5 лет назад
If the while loop was while(curPos < pos), then the return would be last + sLast because the loop would be off by one iteration. But with while(curPos
@drakezen
@drakezen 6 лет назад
Great videos!
@MylesGmail
@MylesGmail 5 лет назад
I tweeted this
@charlitopiaojr.6945
@charlitopiaojr.6945 6 лет назад
You need to explain it more clearly. The instructions quite a bit messed up. You need to define it more clearly
@geekyprogrammer4831
@geekyprogrammer4831 6 лет назад
are u paying him? no. so shut it!
@Christopher_126
@Christopher_126 4 года назад
​@@geekyprogrammer4831 Learn what feedback is, bro. Is this your response whenever someone gives you criticism?
@anmolmultani7720
@anmolmultani7720 6 лет назад
why wouldnt you use the golden ratio, would that be a bad answer at an interview
@HenrikRuep
@HenrikRuep 6 лет назад
Since real numbers are only stored up to a certain precision, the last digits of F_n will certainly be incorrect for n large. Depends on what you need the Fibonaccis for.
@PrajwalCanonShutter
@PrajwalCanonShutter 6 лет назад
hey there, great stuff in ur videos. But the audio volume is low in your videos, please correct it, apart from that everything else is great. ! ! !
@trailerparklife6696
@trailerparklife6696 5 лет назад
Probably include a link to the question? It's hard to follow your accent.
Далее
Friends
00:32
Просмотров 914 тыс.
How I Bombed My First Technical Interview
6:21
Просмотров 6 тыс.
Recursion for Beginners - Fibonacci Numbers
10:16
Просмотров 23 тыс.
Largest Square of 1's in A Matrix (Dynamic Programming)
20:43